0

My situation is similar to this thread :

Associative array, sum values of the same key

However in my case all keys are number. I would like to reduce / combine array where key 0 is similar and make a sum of all other keys.

Here is my original array :

Array
(
[0] => Array
    (
        [0] => 093042
        [1] => 3
        [2] => 0
        [4] => 0
    )

[1] => Array
    (
        [0] => 222032
        [1] => 0
        [2] => 13
        [4] => 0
    )

[2] => Array
    (
        [0] => 222032
        [1] => 0
        [2] => 0
        [4] => 15
    )

[3] => Array
    (
        [0] => 152963
        [1] => 45
        [2] => 0
        [4] => 0
    )

[4] => Array
    (
        [0] => 222032
        [1] => 0
        [2] => 7
        [4] => 0
    )

)

and here is the output i need :

Array
(
    [0] => Array
        (
            [0] => 093042
            [1] => 3
            [2] => 0
            [4] => 0
        )
    [1] => Array
        (
            [0] => 222032
            [1] => 0
            [2] => 20
            [4] => 15
        )
    [2] => Array
        (
            [0] => 152963
            [1] => 45
            [2] => 0
            [4] => 0
        )
)

The solution of other thread is not working because they use the key name and i don't know how i can adapt this to my situation.

Please if you can give me an example of working solution.

REPLY :

For now i try something like that : Take from other thread

$sum = array_reduce($data, function ($a, $b) {
        if (isset($a[$b[0]])) {
             $a[$b[0]]['budget'] += $b['budget'];
        }
        else {
             $a[$b[0]] = $b;
        }
        return $a;
    });

But this example look is only for key named budget but in my case is number and i have 3 key [1] [2] [3] how can't sum key 1,2,4 where key 0 is similar

Community
  • 1
  • 1
  • Welcome to Stack Overflow! This question is a little short on information. Can you share what you have tried, and what problems you have run into? – Jay Blanchard Jul 14 '15 at 19:49
  • Start with writing a `foreach` loop – u_mulder Jul 14 '15 at 19:52
  • Also how's `[1] => Array ( [0] => 222032 [1] => 0 [2] => 20 // hoe this 20 came? [4] => 15 )`. Can you specify? It is in your desired output – Alive to die - Anant Jul 14 '15 at 19:56
  • So replace `budget` key with your keys, huh? – u_mulder Jul 14 '15 at 19:57
  • There's no difference between named and numbered indexes in this problem. Instead of `['budget']` just use `[1]`, `[2]`, and `[4]`. BTW, why don't your inner arrays have `[3]` elements? – Barmar Jul 14 '15 at 20:08
  • like u_mulder said i can replace budget key with my keys like that : `$sum = array_reduce($data, function ($a, $b) { if (isset($a[$b[0]])) { $a[$b[0]][1] += $b[1]; $a[$b[0]][2] += $b[2]; $a[$b[0]][4] += $b[4]; } else { $a[$b[0]] = $b; } return $a; });` But i would like this function be dynamic – Yanick Lafontaine Jul 14 '15 at 20:08

2 Answers2

1

This should work for you:

Basically I just loop through your array and check if there is already an element in $result with the key of the first element of $v. If not I initialize it with an array_pad()'ed array of 0's + the current array of the iteration of the foreach loop.

And after this I loop through each element of $v expect the first one and add it to the result array.

At the end I just reindex the result array with array_values().

<?php

    foreach($arr as $v){

        if(!isset($result[$v[0]]))
            $result[$v[0]] = array_pad([$v[0]], count($v), 0);

        $count = count($v);
        for($i = 1; $i < $count; $i++)
            $result[$v[0]][$i] += $v[$i];


    }

    $result = array_values($result);
    print_r($result);

?>

output:

Array
(
    [0] => Array
        (
            [0] => 093042
            [1] => 3
            [2] => 0
            [3] => 0
        )

    [1] => Array
        (
            [0] => 222032
            [1] => 0
            [2] => 20
            [3] => 15
        )

    [2] => Array
        (
            [0] => 152963
            [1] => 45
            [2] => 0
            [3] => 0
        )

)
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • Your method is working but i need to re index my array before because the key are 0,1,3,4 and not 0,1,2,3 but the key represent the store branch of my companies i'm not sure right now if this will work perfectly in the future. And in array_pad([$v[0]], count($v), 0); You use [$v[0]] as argument ??? I never see this syntax and my Dreamweaver cast me an error for that could you explain this to me. – Yanick Lafontaine Jul 14 '15 at 20:26
  • @Yanick I don't really understand what you mean with the keys? `[$v[0]]` Since in `$v[0]` is your identifier value it's just like e.g. `[222032]`, which is a simple array declaration. (In old syntax: `Array(222032)`) So you can ignore Dreamweaver here. – Rizier123 Jul 14 '15 at 20:29
-1

try this

$data = Array
(
    0 => Array
        (
            0 => 93042,
            1 => 3,
            2 => 0,
            4 => 0,
        ),
    1 => Array
        (
            0 => 222032,
            1 => 0,
            2 => 13,
            4 => 0,
        ),
    2 => Array
        (
            0 => 222032,
            1 => 0,
            2 => 0,
            4 => 15,
        ),
    3 => Array
        (
            0 => 152963,
            1 => 45,
            2 => 0,
            4 => 0,
        ),
    4 => Array
        (
            0 => 222032,
            1 => 0,
            2 => 7,
            4 => 0,
        ),
);

var_dump($data);


// grouping

$tab1 = array();

foreach ($data as $e) {

    if (!isset($tab1[$e[0]])) {
        $tab1[$e[0]] = array();
    }

    $tab1[$e[0]][] = $e;
}

//var_dump($tab1);


// summing

$tabSum = array();

foreach ($tab1 as $id => $t) {

    foreach ($t as $e) {

        unset($e[0]);

        if (!isset($tabSum[$id])) {
            $tabSum[$id] = $e;
        } else {
            foreach ($e as $key => $value) {
                $tabSum[$id][$key] += $value;
            }
        }

    }

}

var_dump($tabSum);
mmm
  • 1,070
  • 1
  • 7
  • 15