Skip to content

Instantly share code, notes, and snippets.

  • Save mendaparadarshit/216f4b0468dec5d68581160d5f37b3bd to your computer and use it in GitHub Desktop.
Save mendaparadarshit/216f4b0468dec5d68581160d5f37b3bd to your computer and use it in GitHub Desktop.
Check if specific array key exists in multidimensional array - PHP
<?php
$array = array(
'21' => array(),
'24' => array(
'22' => array(),
'25' => array(
'26' => array()
)
)
);
var_dump(multiKeyExists($array, 22));
var_dump(multiKeyExists($array, 23));
function multiKeyExists( Array $array, $key ) {
if (array_key_exists($key, $array)) {
return true;
}
foreach ($array as $k=>$v) {
if (!is_array($v)) {
continue;
}
if (array_key_exists($key, $v)) {
return true;
}
}
return false;
}
Output:
bool(true)
bool(false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment