Created
February 14, 2017 16:08
-
-
Save imcarvalho/cbed4eb60f27664a66d7835c10966d88 to your computer and use it in GitHub Desktop.
Simple recursive function to search a multidimensional array by a key, and return all the values of that 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 | |
class SearchMultidimensionalArray | |
{ | |
public static function searchByKey($array, $needle, &$results) { | |
if (is_array($array)) { | |
foreach ($array as $item) { | |
if (isset($item[$needle])) { | |
$results[] = $item[$needle]; | |
continue; | |
} | |
if (is_array($item)) { | |
self::searchByKey($item, $needle, $results); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment