Möchte man ein Array nach bestimmten Kriterien filtern, dann kann man dafür Schleifen verwenden. Eine elegante Alternative zeigt dieses Beispiel:
Code:
$aUnfiltered[] = array('value' => 102, 'id' => 1);
$aUnfiltered[] = array('value' => 152, 'id' => 2);
$aUnfiltered[] = array('value' => 235, 'id' => 3);
$aUnfiltered[] = array('value' => 325, 'id' => 4);
$aUnfiltered[] = array('value' => 782, 'id' => 5);
$aUnfiltered[] = array('value' => 813, 'id' => 6);
$aUnfiltered[] = array('value' => 901, 'id' => 7);
$aFiltered = array_filter($aUnfiltered, function($aValues)
{
if($aValues['value'] > 500)
return $aValues;
});
print_r($aFiltered);
/*
Array
(
[4] => Array
(
[value] => 782
[id] => 5
)
[5] => Array
(
[value] => 813
[id] => 6
)
[6] => Array
(
[value] => 901
[id] => 7
)
)
*/
$aUnfiltered[] = array('value' => 152, 'id' => 2);
$aUnfiltered[] = array('value' => 235, 'id' => 3);
$aUnfiltered[] = array('value' => 325, 'id' => 4);
$aUnfiltered[] = array('value' => 782, 'id' => 5);
$aUnfiltered[] = array('value' => 813, 'id' => 6);
$aUnfiltered[] = array('value' => 901, 'id' => 7);
$aFiltered = array_filter($aUnfiltered, function($aValues)
{
if($aValues['value'] > 500)
return $aValues;
});
print_r($aFiltered);
/*
Array
(
[4] => Array
(
[value] => 782
[id] => 5
)
[5] => Array
(
[value] => 813
[id] => 6
)
[6] => Array
(
[value] => 901
[id] => 7
)
)
*/