I stumbled over a few articles (e.g. this one) and infos that suggest PHP's in_array()
goes through the whole array.
Now there is a possible duplicate of this question here: How does PHP's in_array function work? but the OP was obviously satisfied with the copy/paste of the C language function definition and no further description...
My question however is:
in_array()
really go through the whole array?I tried to look further and go after the ZEND_HASH_FOREACH_KEY_VAL
, but then it got a bit confusing:
php_search_array()
... AKA in_arary()
in PHPZEND_HASH_FOREACH_KEY_VAL
and ZEND_HASH_FOREAC
Only thing I am sure of is that since the ??iteration?? happens on the "C-level" it should be faster than "manual" foreach
...
Does PHP's in_array really go through the whole array?
TLDR; No it doesn't.
The way I read the C implementation:
To answer your question:
php_search_array either invokes Zend RETURN_TRUE (impl: https://github.com/php/php-src/blob/master/Zend/zend_API.h) or sets RET_VAL and performs a C return; afterwards. It both cases, C execution breaks out of the iteration of the array if a match is found.