如何检查多维数组中是否存在特定的数组键

问题描述:

if i'm accessing an index like that

$var = $final_Length_Array[1][0]['id'];

how to check whether the "id" key index exist in the array or not.

i tried to use function array_key_exists but it seems to be working only on one dimension array.

i tried it like if(array_key_exists('id',$final_Length_Array)){ but it didn't work

i also tried isset to check whether there is a result or not but it didn't work also if(isset($final_Length_Array[1][0]['id'])){

any help i'll be appreciate it

如果我正在访问类似的索引 p>

  $  var = $ final_Length_Array [1] [0] ['id']; 
  code>  pre> 
 
 

如何检查数组中是否存在“id”键索引。 p>

我试图使用函数 array_key_exists code>但它似乎只在一个维数组上工作。 p>

我尝试过 比如 if(array_key_exists('id',$ final_Length_Array)){ code>但它不起作用 p>

我也尝试过isset检查是否有结果或 不是但它也不起作用 if(isset($ final_Length_Array [1] [0] ['id'])){ code> p>

任何帮助我' 我会很感激 p> div>

Super hacky solution:

function array_key_exists_recursive($array, $key) {
    return strpos(json_encode($array), "\"" . $key . "\":") !== false;
}

Better solution:

$array = ['a' => ['b' => 'c']];
function array_key_exists_recursive($key, $array) {
    if (array_key_exists($key, $array)) {
        return true;
    }
    foreach($array as $k => $value) {
        if (is_array($value) && array_key_exists_recursive($key, $value)) {
            return true;
        }
    }
    return false;            
}

var_dump(array_key_exists_recursive('b', $array));