I'm having trouble understanding the result of array_search in the following example (and did not find any existing questions discussing this):
<?php
$values = array(
15,
12,
"15",
34,
15 => 25,
"xx" => 15
);
echo "PHP-Version is " . phpversion();
echo "<h1>Array:</h1><pre>";var_dump($values);echo "</pre>";
// sort($values); // remove comment and 15 will be found in ALL cases!
$key = array_search("15",$values);
show_result('(a) Searching "15"');
$key = array_search("15",$values,true);
show_result('(b) Searching "15",true');
$key = array_search(15,$values);
show_result('(c) Searching 15');
$key = array_search(15,$values,false);
show_result('(d) Searching 15,false');
$key = array_search(15,$values,true);
show_result('(e) Searching 15,true');
function show_result($tit) {
global $key,$values;
echo "<h2>$tit</h2>";
if (!$key) {
echo "Not found";
} else {
echo "Found key $key - " . gettype($values[$key]);
}
}
?>
Only search (b) - the strict string-search finds the value, numeric search does not find it. All searches do find it when the array is sorted - but the doc does not mention such a requirement at all. Can someone explain this behaviour?