I have a multidimensional associative array. The top level keys are numbers and the associative arrays inside have strings for both keys and values.
Here is an example of the array dump:
Array
(
[1] => Array
(
[AC21T12-M01] => 54318
)
[2] => Array
(
[AC03T11-F01] => 54480
)
)
So I want to search for 'AC03T11-F01' and return 2 as the key position.
I tried array_search('AC03T11-F01', $array); but it didn't return anything so I'm guessing it's not a straightforward as I thought.
I've used the function below to key the key position when searching for a value, so maybe that could be adapted to search for keys too?
function getParentStack($child, $stack) {
foreach ($stack as $k => $v) {
if (is_array($v)) {
// If the current element of the array is an array, recurse it and capture the return
$return = getParentStack($child, $v);
// If the return is an array, stack it and return it
if (is_array($return)) {
//return array($k => $return);
return $k;
}
} else {
// Since we are not on an array, compare directly
if ($v == $child) {
// And if we match, stack it and return it
return array($k => $child);
}
}
}
// Return false since there was nothing found
return false;
}