Skip to content

Instantly share code, notes, and snippets.

@linxlad
Created May 17, 2016 15:48
Show Gist options
  • Save linxlad/0921fbcbdbb80ca72950c3bca2017f60 to your computer and use it in GitHub Desktop.
Save linxlad/0921fbcbdbb80ca72950c3bca2017f60 to your computer and use it in GitHub Desktop.
Recursively search an array and return the values for a given key.
<?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