Last active
September 26, 2020 15:40
-
-
Save savasdersimcelik/e9e8d2befc305c191b0087ebd5a85a75 to your computer and use it in GitHub Desktop.
PHP Array Deep Search | Returns the value of an element with a key value in an array of uncertain depth.
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
if (! function_exists('array_deep_search')) | |
{ | |
/** | |
* Author : Savaş Dersim ÇELİK | |
* Returns the value of an element with a key value in an array of uncertain depth. | |
* | |
* @param array $array | |
* @param string $key | |
* | |
* @return mixed|false | |
*/ | |
function array_deep_search(array $array, string $key) | |
{ | |
if (isset($array[$key])) | |
{ | |
return $array[$key]; | |
} | |
foreach ($array as $value) | |
{ | |
if (is_array($value)) | |
{ | |
$result = array_deep_search($value, $key); | |
if ($result) | |
{ | |
return $result; | |
} | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment