Find key of parent in array / PHP
Posted
by 106691756905536410593
on Stack Overflow
See other posts from Stack Overflow
or by 106691756905536410593
Published on 2010-03-23T11:40:37Z
Indexed on
2010/03/23
11:43 UTC
Read the original article
Hit count: 378
Perhaps someone can help me out with this one:
I'm using a basic search function to find an array deep within an array. The problem is, once that array is found, I'd also like to return it's parent key. Is there a PHP function that can determine the parent key of an array?
Below is an example of the Search Function... Ideally I'd like to return the array that is found, as well as it's parent key.
function search($array, $key, $value){
$results = array();
if (is_array($array)){
if ($array[$key] == $value){
$results[] = $array;
}
foreach ($array as $subarray){
$results = array_merge($results, search($subarray, $key, $value));
}
}
return $results;
}
© Stack Overflow or respective owner