2

how can I sum values of the key 'NILAI_ANGGARAN'? Note that NILAI_ANGGARAN key is dynamic.

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
            )

    )

I've tried solution from How to sum values of the array of the same key? but it getting this error.

Undefined offset: 1350

Update : This is my desidred result :

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
)

And this is the code I use :

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

Can anyone help me with another solution?

Thx

Community
  • 1
  • 1

5 Answers5

1

You can try this

Array:

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

    )
 );

Get sum values of the key 'NILAI_ANGGARAN' Code:

$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}";

Result:Sum of NILAI_ANGGARAN is :26000000

Uttam Kumar Roy
  • 2,060
  • 4
  • 23
  • 29
0

Let's consider the multi-dimensional array to be $array.

You simply need to use nested loop here.

Try this:

$sum = 0;

foreach ($array as $arr) {

    foreach ($arr as $a) {

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

echo $sum;

Hope this helps.

Peace! xD

Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
0

You also need to check if value with index 'NILAI_ANGGARAN' exists, otherwise PHP shows undefined offset error!

<?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);
aslawin
  • 1,981
  • 16
  • 22
0

you can use advance php for the same. Using RecursiveArrayIterator and RecursiveIteratorIterator it can be done easily:

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

Output

SUM of NILAI_ANGGARAN is 26000000
Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
0

use this code :

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

use this code it will in $sum_arr is array of all element and its also in array format try this it will work 100% :)

Krunal Patel
  • 59
  • 2
  • 6