I have an array of (many) arrays, encoded at different depths. The problem is that I know the key of the element I am searching for, but not the depth of the encoding. So for example it might be
Array ( [1] => Array ( [1] => Array ('abcd' => 'a' ) )
or Array ('abcd' => 'a' )
So is there any way to find that abcd
key?
问问题
369 次
2 回答
0
if (array_key_exists('abcd', $search_array))
于 2013-05-11T03:55:43.237 回答
0
function find_array_key($array,$keytofind){
$found=false;
if (is_array($array)&& $found==false){
foreach($array as $key=>$value){
if (is_array($value)&& $found==false){
find_array_key($array,$keytofind)
}else{
if ($found==false && $key=$keytofind){
$found=$value;
}
}
}
return $found;
}
我没有运行这段代码来测试它,但它应该非常接近一个很好的递归函数,用于你正在尝试做的事情并返回你正在寻找的键的值
于 2013-05-11T01:58:21.193 回答