总结三层数组PHP的内容

This is the array I am using (3 tiers), and I am trying to add the number of sales (nosales) from the first month in the first array to the first month in the second array (Should be 150) and so on through all the variables (months shouldn't be added) so I am left with a two level array with the nosales, salevalue,salecost and saleprofit totals for each month.

Array    (
        [0] => Array
            (
                [1] => Array
                    (
                        [month] => 1
                        [nosales] => 100
                        [salevalue] => 1200
                        [salecost] => 360
                        [saleprofit] => 840
                    )

                [2] => Array
                    (
                        [month] => 2
                        [nosales] => 110
                        [salevalue] => 1320
                        [salecost] => 396
                        [saleprofit] => 924
                    )

            )

        [1] => Array
            (
                [1] => Array
                    (
                        [month] => 1
                        [nosales] => 50
                        [salevalue] => 350
                        [salecost] => 70
                        [saleprofit] => 280
                    )

                [2] => Array
                    (
                        [month] => 2
                        [nosales] => 55
                        [salevalue] => 385
                        [salecost] => 77
                        [saleprofit] => 308
                    )

            )

    )

Now, I have tried looping through them to get them to add together but I am getting a number of errors. Could someone please help?

Here is the script I am using at the moment:

$acc = array_shift($results_array);

foreach ($results_array as $val) {
    foreach ($val as $v) {
        foreach ($v as $key => $v){
        $acc[$key] += $v;
        }
    }
}

Thanks for your help in advance!

Is this what you wanted to do?

$aResultsArray = array(
    0 => array(
        1 => array(
            'month' => 1,
            'nosales' => 100,
            'salevalue' => 1200,
            'saleconst' => 360,
            'saleprofit' => 840,
        ),
        2 => array(
            'month' => 2,
            'nosales' => 110,
            'salevalue' => 1320,
            'saleconst' => 396,
            'saleprofit' => 924,
        ),
    ),
    1 => array(
        1 => array(
            'month' => 1,
            'nosales' => 50,
            'salevalue' => 350,
            'saleconst' => 70,
            'saleprofit' => 280,
        ),
        2 => array(
            'month' => 2,
            'nosales' => 55,
            'salevalue' => 385,
            'saleconst' => 77,
            'saleprofit' => 308,
        ),
    ),
);

$aSum = array();
foreach ($aResultsArray as $mYear => $aMonths) {
    foreach ($aMonths as $mMonth => $aMonth) {

        if (!isset($aSum[$aMonth['month']])) {
            $aSum[$aMonth['month']] = array(
                'month' => $aMonth['month'],
                'nosales' => 0,
                'salevalue' => 0,
                'saleconst' => 0,
                'saleprofit' => 0,

            );
        }

        $aSum[$aMonth['month']]['nosales'] += $aMonth['nosales'];
        $aSum[$aMonth['month']]['salevalue'] += $aMonth['salevalue'];
        $aSum[$aMonth['month']]['saleconst'] += $aMonth['saleconst'];
        $aSum[$aMonth['month']]['saleprofit'] += $aMonth['saleprofit'];

    }
}
var_dump($aSum);