2

This is my array

[Company] => Array
        (
            [0] => Array
                (
                    [date] => 2016-05-28
                    [revenue] => 55
                )

            [1] => Array
                (
                    [date] => 2016-05-28
                    [revenue] => 101
                )

            [2] => Array
                (
                    [date] => 2016-05-29
                    [revenue] => 55
                )

            [3] => Array
                (
                    [date] => 2016-05-29
                    [revenue] => 101
                )

            [4] => Array
                (
                    [date] => 2016-05-30
                    [revenue] => 60
                )

            [5] => Array
                (
                    [date] => 2016-05-30
                    [revenue] => 60
                )

            [6] => Array
                (
                    [date] => 2016-05-31
                    [revenue] => 29
                )

            [7] => Array
                (
                    [date] => 2016-05-31
                    [revenue] => 60
                )

)

I need it to be summed up like this

[Company] => Array
        (
        [0] => Array
         (
           [date] => 2016-05-28
           [revenue] => 151
         )
         *
         *etc.
)

I've tried various methods by in vain. I've tried the below method but didn't work quite well

    foreach($data as $key => $value) {
        foreach ($value as $row) {


            $res[$key][$row['date']] += $row['revenue'];
        }
    }
Henrique Barcelos
  • 7,670
  • 1
  • 41
  • 66
  • What is the output of this method I've tried with? – Henrique Barcelos Jun 07 '16 at 13:12
  • just answered this question here. Could be easliy adapted for your needs http://stackoverflow.com/questions/37675819/php-sum-up-array-entries-where-two-keys-have-the-same-value/37676521#37676521 – atoms Jun 07 '16 at 13:12
  • 1
    Another question: is your array always sorted by date? – Henrique Barcelos Jun 07 '16 at 13:14
  • @HenriqueBarcelos yes – user3496603 Jun 07 '16 at 13:18
  • @atoms taking cue from ur answer, why there's an error of undefined index if I dynamically set the key of an array like this $aSortedArray[$aArray['source']][] = array( 'target' => $aArray['target'], 'total_volume' => $aArray['total_volume'] ); – user3496603 Jun 07 '16 at 13:23
  • setting it as `$aSortedArray[ $aArray['source'] ][] ` would nessicitate that a key with the same value as `$aArray['source']` already exists. Setting it as; `$aSortedArray[][ $aArray['source'] ] ` would create a new key with the value of $aArray['source'] – atoms Jun 07 '16 at 13:28

3 Answers3

1

This should work, its not ideal as your looping over each array twice, as the array gets larger this will exponentially slow down.

$aStartingArray = array();
$aSortedArray   = array();

$aStartingArray[] = array('date'=>'2016-05-28', 'revenue' => 55);
$aStartingArray[] = array('date'=>'2016-05-28', 'revenue' => 101);
$aStartingArray[] = array('date'=>'2016-05-29', 'revenue' => 55);
$aStartingArray[] = array('date'=>'2016-05-29', 'revenue' => 101);
$aStartingArray[] = array('date'=>'2016-05-30', 'revenue' => 60);
$aStartingArray[] = array('date'=>'2016-05-30', 'revenue' => 60);
$aStartingArray[] = array('date'=>'2016-05-31', 'revenue' => 29);
$aStartingArray[] = array('date'=>'2016-05-31', 'revenue' => 60);

// loop through the initial array
foreach ($aStartingArray as $aArray) {

    // set flag to reference if its been dealt with
    $bSet = false;

    // foreach of the sorted values, check if the date matches an entry
    foreach ($aSortedArray as $iPos => $aTempSortedArray) {

        if( $aTempSortedArray['date'] == $aArray['date'] ){

            // match found, add revenue
            $aSortedArray[$iPos]['revenue'] += $aArray['revenue'];

            // set flag to illustrate a dealt with row
            $bSet = true;
        }

    }

    // if still hasnt been dealt with, go add it to the array
    if(!$bSet){
        $aSortedArray[] = array( 'date' => $aArray['date'], 'revenue' => $aArray['revenue'] );
    }

}

var_dump($aSortedArray);
atoms
  • 2,993
  • 2
  • 22
  • 43
  • thanks for the kind words. There are much better and more efficient methods out there. Just be careful if your planning to use it for large data sets – atoms Jun 07 '16 at 13:46
1

I have No doutes with @atoms answer, but it will be done in one foreach

     $newArr = array();
     foreach($arr['Company'] as $key => $value) {
        if(isset($newArr[$value['date']])){
            $newArr[$value['date']]['revenue'] = $newArr[$value['date']]['revenue'] + $value['revenue'];
        } else {
            $newArr[$value['date']] = $value;
        }
    }
    $newArr = array_values($newArr);
DsRaj
  • 2,288
  • 1
  • 16
  • 26
0
$revenue_sum=0;
$elements_sum=0;
   foreach($data as $key => $value) { // 1st dimension
        foreach ($value as $key2 => $value2) { // 2nd dimension
            if ($key2 == "revenue") {
                 $revenue_sum += $value2;
            }
            $elements_sum++;
        }
    }

 echo "Revenue sum: ".$total_sum;
 echo "Elements sum: ".$elements_sum;
khaverim
  • 3,386
  • 5
  • 36
  • 46