Why doesn't in_array work in an array that was created using a loop in PHP?

Why in_array n't in_array work in an array created from php loop?

This code below shows Match found .

 <?php for ($i = 0; $i < 10; ++$i) { $people[] = $i; } if (in_array('test', $people)) { echo "Match found"; } else { echo "Match not found"; } ?> 

and this code below shows Match not found .

 <?php $people = array("0","1","2","3","4","5","6","7","8","9"); if (in_array('test', $people)) { echo "Match found"; } else { echo "Match not found"; } ?> 

How to solve the first code to show Match not found

+5
source share
3 answers

You just change

 $people[] = $i; ---by--> $people[] = "$i"; 

Then you compare the strings

 <?php for ($i = 0; $i < 10; ++$i) { $people[] = "$i"; } if (in_array('test', $people)) { echo "Match found"; } else { echo "Match not found"; } ?> 
+2
source

Because you have integers in your first array, and by default in_array() performs a non-strict type comparison that only has a limited number of type pairs that it takes into account. That way, it will make a quiet tide of an integer with your needle, which results in 0, and it finds it in the array.

To avoid this error, just pass TRUE as the third argument to in_array() for strict type comparisons , e.g.

 if (in_array('test', $people , TRUE )) 
+7
source

You already have answers that will tell you how to get around this. Let me just add a more technical answer why this is happening ....

This behavior is rooted in a peculiar way ZEND_API int compare_function(..) . When you call in_array(needle, haystack, strict) without $ strict = true, this function is used to compare needle with each element in the haystack until a match is found.
Your needle is a string, and the elements in the haystack are integers.
Let's take a (n shorthand) look at compare_function()

 while(1) { switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) { case TYPE_PAIR(IS_LONG, IS_LONG): ZVAL_LONG(result, Z_LVAL_P(op1)>Z_LVAL_P(op2)?1:(Z_LVAL_P(op1)<Z_LVAL_P(op2)?-1:0)); return SUCCESS; [...more CASEs that don't apply on the first iteration of the while-loop...] default: [...some if-elseif cases that do not apply on the first iteration of the while-loop...] } else { zendi_convert_scalar_to_number(op1, op1_copy, result); zendi_convert_scalar_to_number(op2, op2_copy, result); converted = 1; } [...] 

when this code is called for the first time, op1 ~ 'test' and op2~0 . (~ means “roughly”, because internal representation is a little different, but it doesn't really matter.)
But there is no case for TYPE_PAIR (IS_STRING, IS_LONG), so it falls into the default branch :. There are some if-conditions for processing objects, but none of them is an object, so it falls into the part where the operands are converted (once). Again, if-conditon is applied, so it falls into the last else branch, which converts both operands to numbers.
'test' converted to 0 and 0 remains 0 .
Then the while loop executes again, now with TYPE_PAIR (IS_LONG, IS_LONG) - the very first case. And now both operands are equal -> in_array () returns true.

... and yes, I also embarrass this; -)

+2
source

Source: https://habr.com/ru/post/1239957/


All Articles