Search values in a mulitdimensional array
function array_multi_search($mSearch, $aArray, $sKey = "")
{
$aResult = array();
foreach( (array) $aArray as $aValues)
{
if($sKey === "" && in_array($mSearch, $aValues)) $aResult[] = $aValues;
else
if(isset($aValues[$sKey]) && $aValues[$sKey] == $mSearch) $aResult[] = $aValues;
}
return $aResult;
}
$aMyArray[] = array("id" => 1, "name" => "Max Mustermann", "group_id" => 1);
$aMyArray[] = array("id" => 2, "name" => "Karl Aschnikow", "group_id" => 1);
$aMyArray[] = array("id" => 3, "name" => "Rainer Zufall", "group_id" => 2);
//Sucht nach dem Vorkommen der Zahl 2
print_r(array_multi_search(2, $aMyArray));
/*
Ausgabe:
Array
(
[0] => Array
(
[id] => 2
[name] => Karl Aschnikow
[group_id] => 1
)
[1] => Array
(
[id] => 3
[name] => Rainer Zufall
[group_id] => 2
)
*/
Url: http://sklueh.de/2012/11/mit-php-ein-mehrdimensionales-array-durchsuchen/
Language: PHP | User: sklueh | Created: Sep 24, 2013