PHP array_search sequentially returns the first key of an array

I recently noticed a problem when using the array_search function in my code. I am looking for an array of "$ allcraftatts" for the value "sharp". I tried to isolate the problem by installing a two-line experiment:

$testcopy=$allcraftatts; $testsharp=array_search("sharp", $testcopy); 

Using "print_r (get_defined_vars ());" later, I get this result:

 [testcopy] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => Sharp Stone [7] => Sharp Stones [8] => stone [9] => object [10] => sharp [11] => hard [12] => 0 [13] => 0 [14] => 0 [15] => 0 [16] => 0 [17] => 0 [18] => 0 ) [testsharp] => 0 

I made sure that I did not change these variables at any other time.

Now, if I change my code to

 $testcopy=$allcraftatts; unset($testcopy[0]); $testsharp=array_search("sharp", $testcopy); 

it returns "1".

This makes me think that it always returns the first key in the array.

It puzzles me! This is one of those mistakes that makes you afraid of something wrong with the language itself. No matter how doubtful it is, in fact I was eventually forced to look at the source of PHP for what was wrong there, but, unfortunately, I could not understand it.

Seeing that this is a simple function, I will definitely be completely offended by the inevitably simple answer, but for now I just want to get the answer.

+6
source share
3 answers

array_search uses == to compare values โ€‹โ€‹during a search

FORM PHP DOC

If the third strict parameter is set to TRUE, the array_search () function will look for identical elements in the haystack. This means that it also checks the types of needles in the haystack, and the objects must be of the same instance.

Becomes the first element 0 string was converted to 0 during the search

Simple test

 var_dump("sharp" == 0); //true var_dump("sharp" === 0); //false 

The solution uses the strict parameter to search for identical values.

 $testsharp = array_search("sharp", $testcopy,true); ^---- Strict Option var_dump($testsharp); 

Exit

 10 
+8
source

If any key before the search number is zero, then this key is returned, because it performs a โ€œfreeโ€ match in which the data type of the array dominates, and โ€œsharpโ€ (if converted to int ) is considered to be zero. Using strict validation, the correct value was found.

Otherwise by doing

 $testcopy = array_map('strval', $testcopy); 

so that the values โ€‹โ€‹are translated into strings, it also works with "free" validation.

+2
source

Welcome to the wonderful world of free printing. In php, array_search uses lax comparisons ("==") by default, but you can add a third parameter to force strict ("==="). You almost always want a strict one, although there are times when the correct operation is not strict.

check the following:

 $allcraftatts = array(0, 0, 0, 0, 0, 0, "Sharp Stone", "Sharp Stones", "stone", new stdClass(), "sharp", "hard", 0, 0, 0, 0, 0,0 ,0); $testcopy=$allcraftatts; $testsharp=array_search("sharp", $testcopy); $testsharpStrict=array_search("sharp", $testcopy, true); print_r(get_defined_vars()); if(0 == "sharp"){ echo "true for == \n"; }else{ echo "false == \n"; } if(0 === "sharp"){ echo "true for === \n"; }else{ echo "false === \n"; } 

and conclusion:

 [testcopy] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => Sharp Stone [7] => Sharp Stones [8] => stone [9] => stdClass Object ( ) [10] => sharp [11] => hard [12] => 0 [13] => 0 [14] => 0 [15] => 0 [16] => 0 [17] => 0 [18] => 0 ) [testsharp] => 0 [testsharpStrict] => 10 ) true for == false === 
+1
source

All Articles