Last active
September 10, 2020 23:58
-
-
Save mae829/21ab3608ec6b60225ed9883ced2f2f22 to your computer and use it in GitHub Desktop.
Code to check for a key inside a deep array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Deep search array for key. | |
function multi_key_exists( array $arr, $key ){ | |
// is in base array? | |
if ( array_key_exists($key, $arr) ) return true; | |
// check arrays contained in this array. | |
foreach ( $arr as $element ) { | |
if ( is_array( $element ) && multi_key_exists( $element, $key ) ){ | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment