Recursively search the haystack for a specific key and return it's path
/**
* Recursively search the haystack for a specific key and return:
* 1) an array containing the keys that correspond to the path of
* the first found instance path AND
* 2) the value itself.
*
* @param $needle
* The searched value.
* @param array $haystack
* The array.
* @param array $stack
* The stack
*
* @return array|bool
* The array containing the searched key path and value|False if not found.
*/
function array_key_search_recursive($needle, array $haystack, $stack = array()) {
if (array_key_exists($needle, $haystack)) {
array_push($stack, $needle);
return array(
'path' => $stack,
'value' => $haystack[$needle],
);
}
foreach ($haystack as $k => $v) {
if (is_array($v)) {
array_push($stack, $k);
$found = call_user_func(__FUNCTION__, $needle, $v, $stack);
if ($found) {
return $found;
}
array_pop($stack);
}
}
return FALSE;
}