Skip to content

Instantly share code, notes, and snippets.

@adrianthedev
Created May 31, 2012 09:49
Show Gist options
  • Save adrianthedev/2842300 to your computer and use it in GitHub Desktop.
Save adrianthedev/2842300 to your computer and use it in GitHub Desktop.
Search multidimensional arrays PHP
function search($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, search($subarray, $key, $value));
}
return $results;
}
$arr = array(0 => array(id=>1,name=>"cat 1"),
1 => array(id=>2,name=>"cat 2"),
2 => array(id=>3,name=>"cat 1"));
print_r(search($arr, 'name', 'cat 1'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment