For example I have array like below:
Array
(
[0] => Array
(
[a] => 1
[b] => 0
[c] => 1
[d] => 1
)
[1] => Array
(
[a] => 2
[b] => 0
[c] => 3
[d] => 3
)
[4] => Array
(
[a] => 5
[b] => 1
[c] => 3
[d] => 2
)
)
Now I would like to receive only array with the largest [d] value. So in this case:
Array (
[a] => 2
[b] => 0
[c] => 3
[d] => 3
)
What is the easiest and the most optimal way to do it? Thank You!
$res=array();
foreach ($array as $temp)
{
foreach ($temp as $k=>$value)
{
if(!isset($res[$k]))
{
$res[$k]=$value;
}
else
{
$res[$k]=max($value,$res[$k]);
}
}
}
print_r($res);
output Array ( [a] => 5 [b] => 1 [c] => 3 [d] => 3 )
Update Answer
$res=array();
$max=0;
foreach ($array as $temp)
{
if($max<$temp['d'])
{
$max=$temp['d'];
$res=$temp;
}
}
print_r($res);
output Array ( [a] => 2 [b] => 0 [c] => 3 [d] => 3 )