0

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:

Does PHP's 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:

Only thing I am sure of is that since the ??iteration?? happens on the "C-level" it should be faster than "manual" foreach...

Community
  • 1
  • 1
jave.web
  • 13,880
  • 12
  • 91
  • 125
  • Until it finds a match, yes – Mark Baker Apr 15 '17 at 22:31
  • Don't spam tags. This is not related to C. – too honest for this site Apr 15 '17 at 22:31
  • @Olaf It may seem like it, but it is in the end an explanation of C framework function code - in C ... do you still have the same opinion? (This question I think is related as much to PHP as it is to using frameworks in C ... there was no intention to spam :) ) – jave.web Apr 15 '17 at 22:36
  • @MarkBaker would you be so kind and provide some more explanation/"proof" please? :) – jave.web Apr 15 '17 at 22:42
  • The proof is in the source code that you've linked: which part of `for (; _p != _end; _p++) {` are you finding confusing? – Mark Baker Apr 15 '17 at 22:45
  • @MarkBaker Well that is obvious iteration :) I ment more the rest, like what is `(_ht)->arData` – jave.web Apr 15 '17 at 23:02
  • @jave.web: I can read. Just mentioning a C founction is used does not justify the C **language** tag! – too honest for this site Apr 15 '17 at 23:04
  • @Olaf Ok, you are telling me explanation of a C language function is not C language related, I see no further discussion here. Thank you for your input though. – jave.web Apr 15 '17 at 23:05
  • That's just childish behaviour. Your question is actually about the **behaviour**; a specific implementation is not relevant for your question. – too honest for this site Apr 15 '17 at 23:08
  • @Olaf If you want to accuse people of childish behaviour, you surely should start with the man in the mirror. Specific implementation is very relevant, if it would be implemented in bricks it surely could have house-like complexity, not a binary one, I know this is ridiculous example, but I am just making the point. Till now you've given me only reasons why to put the C tag back, not why it should stay removed. – jave.web Apr 15 '17 at 23:14

1 Answers1

1

Does PHP's in_array really go through the whole array?

TLDR; No it doesn't.

The way I read the C implementation:

  1. ZEND_HASH_FOREACH_KEY_VAL or rather ZEND_HASH_FOREACH iterates over the array data bucket with a pointer to the current element.
  2. The element pointer is assigned to the variable entry in void php_search_array for each iteration.
  3. When a match is found, The PHP list item itself or PHP bool is returned by the engine depending on the behavior argument given to the function.

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.

  • `iterates over the array data bucket` and what is in the bucket? All array items or some subset? – jave.web Apr 15 '17 at 23:04
  • 1
    That would be all array items. Worst case look up time for in_array() is O(n) where n is the total number of elements. It is basically a simple linear search. But best case is still O(1), given that the first element of the array matches. – Johan Thomsen Apr 15 '17 at 23:08
  • Thank you, you've fully understood the question and explained the last thing to make it complete :) – jave.web Apr 15 '17 at 23:10
  • BTW: I would add to TLDR, that it does go "step by step" ***until*** it finds the match. – jave.web Apr 19 '17 at 21:14