I have written the following function to search an multi-dimensional array by key, but if I call the function with key uri, it appends arrays having key 0 to the found array. What’s wrong?
function search_arr($array, $key, &$found) {
foreach ($array as $k => $each) {
if ($k == $key) {
// output of "print $k = $key" is "0 = uri"
$found[] = $each;
}
if (is_array($each)) {
search_arr($each, $key, $found);
}
}
}
Use === to force a value and type match. You get zeros because comparing 0 and “uri” is
true– think of them as both evaluating to 0. For complete details of how PHP handles these ‘loose’ comparisons, see this section of the manual.