Skip to content

Instantly share code, notes, and snippets.

@imcarvalho
Created February 14, 2017 16:08
Show Gist options
  • Save imcarvalho/cbed4eb60f27664a66d7835c10966d88 to your computer and use it in GitHub Desktop.
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
<?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