php - 如何对多维数组的值求和

时间:2016-03-23 05:52:09

标签: php arrays multidimensional-array sum

如何对键' NILAI_ANGGARAN '的值求和?请注意,NILAI_ANGGARAN键是动态的。

数组:

[1350] => Array
    (
        [495] => Array
            (
                [NILAI_ANGGARAN] => 11000000
                [NILAI_PPN] => 1000000
                [PFK] => 0
                [TAPERUM] => 0
                [LAIN_LAIN] => 0
                [NILAI_PPH21] => 500000
                [NILAI_PPH22] => 0
                [NILAI_PPH23] => 0
                [NILAI_PPH4_2] => 0
                [DENDA] => 0
                [NILAI_BERSIH] => 10500000
            )

    )

[1300] => Array
    (
        [488] => Array
            (
                [NILAI_ANGGARAN] => 15000000
                [NILAI_PPN] => 1500000
                [PFK] => 0
                [TAPERUM] => 0
                [LAIN_LAIN] => 0
                [NILAI_PPH21] => 0
                [NILAI_PPH22] => 450000
                [NILAI_PPH23] => 300000
                [NILAI_PPH4_2] => 0
                [DENDA] => 0
                [NILAI_BERSIH] => 15750000
            )

    )

我尝试过来自How to sum values of the array of the same key?的解决方案,但却收到了此错误。

Undefined offset: 1350

更新: 这是我的最终结果:

Array 
(
    [NILAI_ANGGARAN] => 26000000
    [NILAI_PPN] => 2500000
    [PFK] => 0
    [TAPERUM] => 0
    [LAIN_LAIN] => 0
    [NILAI_PPH21] => 500000
    [NILAI_PPH22] => 450000
    [NILAI_PPH23] => 300000
    [NILAI_PPH4_2] => 0
    [DENDA] => 0
    [NILAI_BERSIH] => 26250000
)

这是我使用的代码:

$bruto = array();
foreach($array as $data => $key) {
    foreach($key as $k => $value) {
         foreach($value as $v => $isi) {
              $bruto[$k]+=$value;
         }
    }
}
print_r($bruto);

任何人都可以帮我解决另一个问题吗?

THX

5 个答案:

答案 0 :(得分:1)

你可以试试这个

阵列:

$multi_dimentional_array = array (
'1350' => array
    (
        '495' => array
            (
                'NILAI_ANGGARAN' => 11000000,
                'NILAI_PPN' => 1000000,
                'PFK' => 0,
                'TAPERUM' => 0,
                'LAIN_LAIN' => 0,
                'NILAI_PPH21' => 500000,
                'NILAI_PPH22' => 0,
                'NILAI_PPH23' => 0,
                'NILAI_PPH4_2' => 0,
                'DENDA' => 0,
                'NILAI_BERSIH' => 10500000
            )

    ),
'1300' => array
    (
        '488' => array
            (
                'NILAI_ANGGARAN' => 15000000,
                'NILAI_PPN' => 1500000,
                'PFK' => 0,
                'TAPERUM' => 0,
                'LAIN_LAIN' => 0,
                'NILAI_PPH21' => 0,
                'NILAI_PPH22' => 450000,
                'NILAI_PPH23' => 300000,
                'NILAI_PPH4_2' => 0,
                'DENDA' => 0,
                'NILAI_BERSIH' => 15750000
            )

    )
 );

获取密钥'NILAI_ANGGARAN'代码的总和值:

$NILAI_ANGGARAN_TOTAL = 0;
foreach( $multi_dimentional_array as $fkey=>$smarray )
{
    foreach ($smarray as $skey => $value) {
        // Empty check 
        if ( !empty( $value['NILAI_ANGGARAN'] ) ){
            $NILAI_ANGGARAN_TOTAL += $value['NILAI_ANGGARAN'];
        }       
    }

}
echo "Sum of NILAI_ANGGARAN is :{$NILAI_ANGGARAN_TOTAL}";

结果:Sum of NILAI_ANGGARAN is :26000000

答案 1 :(得分:0)

让我们将多维数组视为$ array。

你只需要在这里使用嵌套循环。

试试这个:

$sum = 0;

foreach ($array as $arr) {

    foreach ($arr as $a) {

        $sum += $a['NILAI_ANGGARAN'];
    }
}

echo $sum;

希望这有帮助。

和平!的xD

答案 2 :(得分:0)

您还需要检查索引'NILAI_ANGGARAN'的值是否存在,否则PHP会显示未定义的偏移错误!

<?php
$array = array (
'1350' => array
    (
        '495' => array
            (
                'NILAI_ANGGARAN' => 11000000,
                'NILAI_PPN' => 1000000,
                'PFK' => 0,
                'TAPERUM' => 0,
                'LAIN_LAIN' => 0,
                'NILAI_PPH21' => 500000,
                'NILAI_PPH22' => 0,
                'NILAI_PPH23' => 0,
                'NILAI_PPH4_2' => 0,
                'DENDA' => 0,
                'NILAI_BERSIH' => 10500000
            )

    ),
'1300' => array
    (
        '488' => array
            (
                'NILAI_ANGGARAN' => 15000000,
                'NILAI_PPN' => 1500000,
                'PFK' => 0,
                'TAPERUM' => 0,
                'LAIN_LAIN' => 0,
                'NILAI_PPH21' => 0,
                'NILAI_PPH22' => 450000,
                'NILAI_PPH23' => 300000,
                'NILAI_PPH4_2' => 0,
                'DENDA' => 0,
                'NILAI_BERSIH' => 15750000
            )

    )
 );


$sum = 0;
foreach($array as $key => $subarray)
{
    foreach($subarray as $subsubarrray)
    {
        if(isset($subsubarrray['NILAI_ANGGARAN'])) //check if isset
            $sum += $subsubarrray['NILAI_ANGGARAN'];
    }
}

var_dump($sum);

答案 3 :(得分:0)

你可以使用提前的PHP相同。使用RecursiveArrayIteratorRecursiveIteratorIterator可以轻松完成:

$multi_dimentional_array = array (
    '1350' => array
    (
        '495' => array
        (
            'NILAI_ANGGARAN' => 11000000,
            'NILAI_PPN' => 1000000,
            'PFK' => 0,
            'TAPERUM' => 0,
            'LAIN_LAIN' => 0,
            'NILAI_PPH21' => 500000,
            'NILAI_PPH22' => 0,
            'NILAI_PPH23' => 0,
            'NILAI_PPH4_2' => 0,
            'DENDA' => 0,
            'NILAI_BERSIH' => 10500000
        )

    ),
    '1300' => array
    (
        '488' => array
        (
            'NILAI_ANGGARAN' => 15000000,
            'NILAI_PPN' => 1500000,
            'PFK' => 0,
            'TAPERUM' => 0,
            'LAIN_LAIN' => 0,
            'NILAI_PPH21' => 0,
            'NILAI_PPH22' => 450000,
            'NILAI_PPH23' => 300000,
            'NILAI_PPH4_2' => 0,
            'DENDA' => 0,
            'NILAI_BERSIH' => 15750000
        )

    )
);

$sum = 0;
$k_value = 'NILAI_ANGGARAN';
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($multi_dimentional_array));

foreach ($iterator as $key => $value) {
    if($key == $k_value){
        $sum +=$value;
    }
}


echo "SUM of $k_value is $sum";

<强>输出

SUM of NILAI_ANGGARAN is 26000000

答案 4 :(得分:0)

使用此代码:

$sum_arr = array();
foreach($main as $m_item){
    foreach($sub as $s_item){
        foreach($s_item as $skey => $value){
            $sum_arr[$skey] += $value;
        }
    }
}

$sum_arr中使用此代码是所有元素的数组,也是数组格式 试试这将100%工作:)