Dr.Neo
Dr.Neo

Reputation: 1280

Sum values in Multiple Dimensional array that have same key value

i have a Multiple Dimensional array and i need to sum up values which have the same keys .
print_r($inputs)

Array
(
    [0] => Array
        (
            [id] => colors
            [power] => Array
                (
                    [green] => 12
                    [red] => 5
                    [orange] => 9
                    [black] => 6
                    [white] => 5
                    [blue] => 11
                )

        )

    [1] => Array
        (
            [id] => colors
            [power] => Array
                (
                    [green] => 20
                    [red] => 40
                    [orange] => 80
                    [black] => 60
                    [white] => 100
                    [blue] => 110
                )

        )
    [2] => Array
        (
            [id] => glossycolor
            [power] => Array
                (
                    [green] => 20
                    [red] => 40
                    [orange] => 80
                    [black] => 60
                    [white] => 100
                    [blue] => 110
                )

        )

)

i need the result to be like

Array
(
    [0] => Array
        (
            [id] => colors
            [power] => Array
                (
                    [green] => 32
                    [red] => 45
                    [orange] => 89
                    [black] => 66
                    [white] => 105
                    [blue] => 121
                )

        )

    [1] => Array
        (
            [id] => glossycolor
            [power] => Array
                (
                    [green] => 20
                    [red] => 40
                    [orange] => 80
                    [black] => 60
                    [white] => 100
                    [blue] => 110
                )

        )

)

i tried to use array_shift to sort the values and sum the sub array values but i failed

$finalRate = array_shift($inputs);
                foreach ($inputs as $val) {
                    foreach ($val as $key => $val) {
                        $finalRate[$key] += $val;
                    }
                }

but failed and return empty array .

Upvotes: 1

Views: 591

Answers (3)

Mahesh V
Mahesh V

Reputation: 358

$array1 = array_slice($input,0,1);  //slicing first value of $input i.e Array([0]=>array)
$array2 = array_slice($input,1,1);  //slicing second value of $input i.e Array([1]=>array)
$array = array_sum_values($array1,$array2); //summing values of two arrays
$input = array_splice($input,0,2,$array) //Removing [0] and [1] from $input and replacing with $array.

Please refer PHP manual for more details.

Upvotes: 1

smassey
smassey

Reputation: 5931

This probably isn't the most efficient way for going about this.. but it works and was easy to implement:

$arr = array(
    0 => array(
        'id' => 'colors',
        'power' => array(
            'green' => 12,
            'red' => 5,
            'orange' => 9,
            'black' => 6,
            'white' => 5,
            'blue' => 11,
        ),
    ),
    1 => array(
        'id' => 'colors',
        'power' => array(
            'green' => 20,
            'red' => 40,
            'orange' => 80,
            'black' => 60,
            'white' => 100,
            'blue' => 110,
        ),
    ),
    2 => array(
        'id' => 'glossycolors',
        'power' => array(
            'green' => 20,
            'red' => 40,
            'orange' => 80,
            'black' => 60,
            'white' => 100,
            'blue' => 110,
        ),
    ),
);

foreach ( $arr as $k1 => $v1 ) {
    foreach ( $arr as $k2 => $v2 ) {
        if ( $k1 === $k2 ) continue;
        if ( ! isset( $arr[ $k1 ] ) || ! isset( $arr[ $k2 ] ) ) continue;
        if ( $v1['id'] === $v2['id'] ) {
            foreach ( $v2['power'] as $power_k => $power_v ) {
                $arr[$k1]['power'][$power_k] += $power_v;
            }
            unset( $arr[$k2] );
        }
    }
}

print_r( $arr );

And this results in:

Array
(
    [0] => Array
        (
            [id] => colors
            [power] => Array
                (
                    [green] => 32
                    [red] => 45
                    [orange] => 89
                    [black] => 66
                    [white] => 105
                    [blue] => 121
                )

        )

    [2] => Array
        (
            [id] => glossycolors
            [power] => Array
                (
                    [green] => 20
                    [red] => 40
                    [orange] => 80
                    [black] => 60
                    [white] => 100
                    [blue] => 110
                )

        )

)

So basically, it loops through the same array twice and sums the values of common 'id' elements, then removes the second copy from the array leaving only the original with the sum of the later. Cheers

Upvotes: 0

guessimtoolate
guessimtoolate

Reputation: 8632

Assuming your arrays have always the same structure I'd go with:

$outcome = array();
foreach ($colors as $array) {
    $id = $array['id'];

    if (array_key_exists($id, $outcome)) {
        foreach ($array['power'] as $color => $value) {
            $outcome[$id]['power'][$color] += $value;
        }
        continue;
    }
    $outcome[$array['id']] = $array;
}
array_values($outcome);

Upvotes: 1

Related Questions