Created
May 17, 2016 15:48
-
-
Save linxlad/0921fbcbdbb80ca72950c3bca2017f60 to your computer and use it in GitHub Desktop.
Recursively search an array and return the values for a given key.
This file contains hidden or 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 | |
/** | |
* @param $needle | |
* @param $haystack | |
* | |
* @return array | |
*/ | |
public function recursiveArraySearch($needle, $haystack) | |
{ | |
foreach ($haystack as $key => $value) { | |
if ($key === $needle) { | |
return $value; | |
} elseif (is_array($value)) { | |
$result = $this->recursiveArraySearch($needle, $value); | |
if ($result !== false){ | |
return $result; | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment