0

I have the following array:

[0] => Array

    (
        [id] => 1
        [uid] => 50
        [sum1] => 1
        [sum2] => 2
       

    )

[1] => Array

    (
        [id] => 2
        [uid] => 50
        [sum1] => 2
        [sum2] => 4
       

    )
[2] => Array

    (
        [id] => 3
        [uid] => 51
        [sum1] => 3
        [sum2] => 5
       

    )

As you can see, on some of those indexes, [uid] is the same. The length and data of the array is dynamic. what i need to do is merge the indexes that have the same value for[uid] and sum the values for the other keys:

[0] => Array

    (
        [id] => 2
        [uid] => 50
        [sum1] => 3
        [sum2] => 6
       

    )
[1] => Array

    (
        [id] => 3
        [uid] => 51
        [sum1] => 3
        [sum2] => 5
       

    )

But for the life of me, i can't figure out how to do that.

Any help appreciated!

  • Does this answer your question? [How to sum all column values in multi-dimensional array?](https://stackoverflow.com/questions/1496682/how-to-sum-all-column-values-in-multi-dimensional-array) – B001ᛦ Mar 31 '21 at 21:31
  • Create a new array, iterate over the original one, for each item check if an object having same uid is present (using array_column and array_search)... if present increment values, otherwise append. Not difficult. – Salman A Mar 31 '21 at 21:31
  • Actually, B001, it dosen't because inthat example are all idexes to be summed up or in my case, i need to sum only the matching keys. – Leonida-Alexandru Diaconu Mar 31 '21 at 21:56
  • @SalmanA, that is a good idea. i'll try to implemet that. thanks. – Leonida-Alexandru Diaconu Mar 31 '21 at 21:57

2 Answers2

1

You can do it like this way,

<?php
$mainArray = [ 
    0 => Array
    (
        'id' => 1,
        'uid' => 50,
        'sum1' => 1,
        'sum2' => 2
    ),
    1 => Array
    (
        'id' => 2,
        'uid' => 50,
        'sum1' => 2,
        'sum2' => 4
    ),
    2 => Array
    (
        'id' => 3,
        'uid' => 51,
        'sum1' => 3,
        'sum2' => 5
    )
];
$newArray=[];
foreach ($mainArray as $value) {
    if(!array_key_exists($value['uid'], $newArray)){
        // Store first time
        $newArray[$value['uid']] = $value;
    }else{
        // Already exist, then sum and replace it
        $newArray[$value['uid']]['id'] = $value['id'];
        $newArray[$value['uid']]['sum1'] += $value['sum1'];// Sum with previous value
        $newArray[$value['uid']]['sum2'] += $value['sum2'];// Sum with previous value
    }
}
$newArray = array_values($newArray);// Reset indexes, Start the array index with zero
print_r($newArray);// To see the output

Output:


Array
(
    [0] => Array
        (
            [id] => 2
            [uid] => 50
            [sum1] => 3
            [sum2] => 6
        )

    [1] => Array
        (
            [id] => 3
            [uid] => 51
            [sum1] => 3
            [sum2] => 5
        )
)

Anisur Rahman
  • 644
  • 1
  • 4
  • 17
0

I think you are correct. Actually, i solved the problem using something similar:

$sumArray = Array();
foreach ($array1 as $item){

        if(isset($sumArray[$item['uid']])){          
          $sumArray[$item['idPartener']]['sum1'] += $item['sum1'];
          $sumArray[$item['idPartener']]['sum2'] += $item['sum2'];

        }else{          
          $sumArray[$item['uid']] = Array(
            'id' => $item['id'],             
            'sum1' => $item['sum1'],
            'sum2' => $item['sum2'] ,
          );
        }

    }

Thanks all for your help!