-1

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!

Gemmi
  • 1,252
  • 2
  • 13
  • 26

3 Answers3

3
$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 )

Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38
  • That's not what I meant. What if array looks like: '$array = Array ( 0 => Array ( "a" => "1", "b" => "0", "c" => "1", "d" => "1" ), 1 => Array ( "a" => "1", "b" => "5", "c" => "1", "d" => "2" ), 4 => Array ( "a" => "1", "b" => "0", "c" => "1", "d" => "3" ) );' Output is: Array ( [a] => 1 [b] => 5 [c] => 1 [d] => 3 ) But I would like to receive : Array ( [a] => 1 [b] => 0 [c] => 1 [d] => 3 ) – Gemmi Feb 05 '15 at 19:29
1
$array = 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 
    ));

    uasort ( $array, function ($a, $b) {
            return $a['d'] > $b['d'];
        }
    );

$valueYouAreSearchingFor = $array[0];

I used uasort to sort the array using an anonymous function. I'd have to test if it's faster than a ForEach loop, but it deffinitely is more fun (at least if you ask me :) )

If you don't want to change the original array, make a copy and sort the copy.

PS: Google is your friend...

Ziga Petek
  • 3,953
  • 5
  • 33
  • 43
  • I would take this concise approach over the accepted answer any day, regardless of any marginal performance gains. – Drumbeg Feb 05 '15 at 20:27
0
$subArray1 = array('a' => 1, 'b' => 0, 'c' => 1, 'd' => 1);
$subArray2 = array('a' => 2, 'b' => 0, 'c' => 3, 'd' => 3);
$subArray3 = array('a' => 5, 'b' => 1, 'c' => 3, 'd' => 2);
$data = array($subArray1, $subArray2, $subArray3);

$resultArray = array();
/*
Turn off 'notification' level errors or add 'isset' checks
*/
foreach($data as $subArray)
{
    foreach($subArray as $key => $value)
    {
        $resultArray[$key] = max($resultArray[$key], $value);
    }
}
var_dump($resultArray);
JerzySkalski
  • 624
  • 7
  • 9
  • Your result is Array ( [a] => 5 [b] => 1 [c] => 3 [d] => 3 ), not Array ( [a] => 2 [b] => 0 [c] => 3 [d] => 3 ), – Gemmi Feb 05 '15 at 19:45