检查特定阵列键多维数组存在 - PHP [英] Check if specific array key exists in multidimensional array - PHP
本文介绍了检查特定阵列键多维数组存在 - PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个multidimenional阵列例如(这可能是很多层次深):
I have a multidimenional array e.g. (this can be many levels deep):
$array = Array (
[21] => Array ( )
[24] => Array (
[22] => Array ( )
[25] => Array (
[26] => Array ( )
)
)
)
我想通过它来循环查看某个项是否存在:
I am trying to loop through it to see if a certain key exists:
$keySearch=22; // key seraching for
function findKey($array, $keySearch) {
foreach ($array as $item){
if (isset($item[$keySearch]) && false === findKey($item[$keySearch], $item)){
echo 'yes, it exists';
}
}
}
findKey($array, $keySearch);
但觉得没有什么。有没有在循环的错误?
But it finds nothing. Is there an error in the loop?
推荐答案
我打了你的code得到它的工作:
I played with your code to get it working :
function findKey($array, $keySearch)
{
foreach ($array as $key => $item) {
if ($key == $keySearch) {
echo 'yes, it exists';
return true;
}
else {
if (is_array($item) && findKey($item, $keySearch)) {
return true;
}
}
}
return false;
}
这篇关于检查特定阵列键多维数组存在 - PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文