总结三层数组PHP的内容

时间:2013-08-18 05:44:01

标签: php multidimensional-array foreach

这是我使用的阵列(3层),我试图将第一个阵列中第一个月的销售数量(nosales)添加到第二个阵列中的第一个月(应该是150)所以通过所有变量(不应该添加月份)所以我留下了一个两级数组,每个月都有nosales,salevalue,salecost和saleprofit总数。

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
                    )

            )

    )

现在,我已经尝试循环遍历它们以使它们一起添加但我收到了一些错误。有人可以帮忙吗?

这是我目前正在使用的脚本:

$acc = array_shift($results_array);

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

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这是你想做的吗?

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