Skip to content

Instantly share code, notes, and snippets.

@miguelramos
Created March 6, 2012 23:31
Show Gist options
  • Save miguelramos/1989766 to your computer and use it in GitHub Desktop.
Save miguelramos/1989766 to your computer and use it in GitHub Desktop.
PHP: Search element in array recursive
<?php
/**
* Function to search for key/value in recursive mode. If last arg is true
* it returns it's value or key. By default give true or false if key/value have been found.
*
* @param array $array Array to search
* @param string $search What to search
* @param string $mode Mode search for value or key
* @param boolean $return Return value or key
* @return boolean or string|array|object whatever
*/
function element_search(array $array, $search, $mode = 'value', $return = false) {
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $key => $value) {
if ($search === ${${"mode"}}){
return $return ? ${${"mode"}} : true;
}
}
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment