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.
source share