For an array such as:
$array = array(
735 => array('name'=>'Alpha', 'num'=>1),
584 => array('name'=>'Beta', 'num'=>4),
857 => array('name'=>'Gamma', 'num'=>1),
982 => array('name'=>'Delta', 'num'=>2)
);
what would be the best way to filter elements with least value of num. That is, in this case, the solution would be the following array:
array(
735 => array('name'=>'Alpha', 'num'=>1),
857 => array('name'=>'Gamma', 'num'=>1)
);
I'm aware that this can be done via a foreach loop and keeping track of the least value but I was hoping there would be some array function which would do the job.
My current approach is:
$num_values = array();
foreach($array as $id => $meta)
{
$num_values[] = $meta['num'];
}
$min_num_value = min($num_values);
$filtered_array = array();
foreach($array as $id => $meta)
{
if($meta['num'] == $min_num_value)
{
$filtered_array[$id] = $meta;
}
}
print_r($filtered_array);
which, as you can see, is clearly not the best way to go about the task.