0

I have following array structure. I'd like to get max value in array, and if values in array are same to unset this array.

   Array(

        [499670] => Array
            (
                [499670] => 1299.00
                [503410] => 1299.00
                [528333] => 1299.00
                [645862] => 0.00
            )

        [499671] => Array
            (
                [499671] => 1149.00
                [503408] => 1149.00
                [528329] => 1500.00
                [645858] => 0.00
            )

        [499672] => Array
            (
                [499672] => 0.00
                [503406] => 0.00
                [528324] => 0.00
                [645850] => 0.00
            )
)

I want to get the following result

   Array(

                [499670] => 1299.00 >>> one of values in first array
                [528329] => 1500.00 >>> max value in second array
                {third array was removed, because all values are same}

)
dido
  • 2,330
  • 8
  • 37
  • 54

1 Answers1

3

Iterate through your array, use array_unique() to check if all the values are the same. If not, find the max value using max():

$result = array();

foreach ($data as $key => $subarr) {
    if (count(array_unique($subarr)) === 1) {
        unset($data[$key]);
    } else {
        $result[] = max($subarr);
    }
}

print_r($result);

Output:

Array
(
    [0] => 1299.00
    [1] => 1500.00
)

Demo

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • 1
    I believe the `, PHP_EOL` has nothing to do here ;-) – svvac Mar 20 '14 at 16:40
  • @swordofpain: That was initially used with the `echo` statement to output them in new lines. But I've edited the answer to use an array instead ;) – Amal Murali Mar 20 '14 at 16:41