-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'];
        }
Asfandyar Khan
  • 1,677
  • 15
  • 34
  • 1
    @ficuscr That question doesn't need the sums grouped by another column. – Barmar Aug 03 '17 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 – Asfandyar Khan Aug 03 '17 at 19:45
  • Retracted. Sorry, see a lot of these and they are always `array_sum` / `array_map` solutions. Glossed over it. – ficuscr Aug 03 '17 at 19:45
  • You can use: $result = array(); foreach($Arr1 as $data) { $result [ $Arr1 ['date'] ] += $Arr1 ['mal']; } – Tanseer UL Hassan Aug 03 '17 at 19:47
  • @Barmar, its not a duplicate of that question too, That question answer gives me error `Illegal string offset 'date'` – Asfandyar Khan Aug 03 '17 at 19:48
  • @TanseerULHassan your answer gives `Undefined index: 2015-12-00` – Asfandyar Khan Aug 03 '17 at 19:49
  • @AsfandyarKhan You must have done something wrong, it should work. Update your question with the code you tried. – Barmar Aug 03 '17 at 19:50
  • I see the problem, you're not creating `$array_1` properly. You're just overwriting it each time through the loop, not creating a 2-dimensional array. – Barmar Aug 03 '17 at 19:51
  • Just use `$Arr1`, there's no need for the new array `$array_1`. – Barmar Aug 03 '17 at 19:52
  • How can i create it properly then? @barmar – Asfandyar Khan Aug 03 '17 at 19:52
  • `$Arr1` is already created properly. – Barmar Aug 03 '17 at 19:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150999/discussion-between-asfandyar-khan-and-barmar). – Asfandyar Khan Aug 03 '17 at 19:55
  • @Barmar This question is not duplicate and then question you mention is other thing which sum on bases of key but his question sum on the basis of key value like data value 2015-12-00 so reopen it. Also the second array is expected result array so that is not yet created. – Tanseer UL Hassan Aug 03 '17 at 19:59
  • Related: [Create multidimensional array from database query in PHP](https://stackoverflow.com/q/52059113/2943403) and [Php multi-dimensional array from mysql result](https://stackoverflow.com/q/5053857/2943403) – mickmackusa Apr 16 '23 at 07:50

1 Answers1

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

Barmar
  • 741,623
  • 53
  • 500
  • 612