Created
March 6, 2012 23:31
-
-
Save miguelramos/1989766 to your computer and use it in GitHub Desktop.
PHP: Search element in array recursive
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 | |
/** | |
* 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