-1

This question is not a duplicate of other. it is different,This is current code, I am trying to sum the values of a array based on the same key value of date.

foreach ($TablesArr as $tr) {
     $criteria = new CDbCriteria;
     $criteria->select = 'date,mal';
     $Arr1 = $tr::model()->findAll($criteria);
     $array_1 = array();
     foreach ($Arr1 as $a1) {
          $array_1['date'] = $a1['date'];
          $array_1['mal'] = $a1['mal'];
     }
}

My Current output is:

Array
(
    [date] => 2015-11-00
    [mal] => 35
)
Array
(
    [date] => 2015-12-00
    [mal] => 20
)
Array
(
    [date] => 2016-01-00
    [mal] => 30
)
Array
(
    [date] => 2016-01-00
    [mal] => 10
)
Array
(
    [date] => 2015-11-00
    [mal] => 50
)

If the date is same, then sum the value of mal in the array. For example, In the above array, the date 2015-11-00 appears twice or more than twice (5 or 10 times etc) then sum all of the values of it for that date. Currently it appears twice so for 2015-11-00 the mal values will be 35+50=85. I want my output to be like this below:

Array
(
    [date] => 2015-11-00
    [mal] => 85
)
Array
(
    [date] => 2015-12-00
    [mal] => 20
)
Array
(
    [date] => 2016-01-00
    [mal] => 40
)

I have tried:

$result = array();

        foreach($Arr1 as $data) {
            $result[ $data['date'] ] += $data['mal'];
        }
14
  • 1
    @ficuscr That question doesn't need the sums grouped by another column. Commented Aug 3, 2017 at 19:43
  • Its not a duplicate of other question, I already lot of time on SOF, can't find any value that worked for me @ficuscr Commented Aug 3, 2017 at 19:45
  • Retracted. Sorry, see a lot of these and they are always array_sum / array_map solutions. Glossed over it. Commented Aug 3, 2017 at 19:45
  • You can use: $result = array(); foreach($Arr1 as $data) { $result [ $Arr1 ['date'] ] += $Arr1 ['mal']; } Commented Aug 3, 2017 at 19:47
  • @Barmar, its not a duplicate of that question too, That question answer gives me error Illegal string offset 'date' Commented Aug 3, 2017 at 19:48

1 Answer 1

2

Your $array_1 variable doesn't contain all the values from the original $Arr1 array. You're overwriting the same elements each time through the loop, so it just contains the last item from $Arr1. These lines:

$array_1['date'] = $a1['date'];
$array_1['mal'] = $a1['mal'];

should be:

$array_1[] = array('date' => $a1['date'], 'mal' => $a1['mal']);

Another problem is that you're resetting $array_1 each time through the outer loop.

You can also use array_merge to combine the arrays, instead of the loop:

$array_1 = array();
foreach ($TablesArr as $tr) {
     $criteria = new CDbCriteria;
     $criteria->select = 'date,mal';
     $Arr1 = $tr::model()->findAll($criteria);
     $array_1 = array_merge($array_1, $Arr1);
}

You can use one of the solutions in php group by SUM using multi dimensional array to create an associative array with the sums grouped by date. You can turn this into a 2-dimensional array with another loop:

$newresult = array();
foreach ($result as $date => $mal) {
    $newresult[] = array('date' => $date, 'mal' => $mal);
}

DEMO

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.