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'];
        }
    }
6
  • What is the output of this method I've tried with? Commented Jun 7, 2016 at 13:12
  • just answered this question here. Could be easliy adapted for your needs stackoverflow.com/questions/37675819/… Commented Jun 7, 2016 at 13:12
  • 1
    Another question: is your array always sorted by date? Commented Jun 7, 2016 at 13:14
  • @HenriqueBarcelos yes Commented Jun 7, 2016 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'] ); Commented Jun 7, 2016 at 13:23

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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
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);

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.