Sum deep column values of an array with 3 levels of depth

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 all column values in multi-dimensional array? 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);

Upvotes: 2

Views: 173

Answers (6)

mickmackusa
mickmackusa

Reputation: 48070

First, I'll offer a script to complete the task intended by your coding attempt. Because you first level keys are numeric strings, it will be safe to flatten your 3-level structure down to a 2-level structure via a "spread&merge" technique. This will not disrupt the keys in the 3rd level where the subsequent array column isolation will be performed.

Using array_column() on the temporary 2d array will avoid the need to check if a given column exists in each row. Finally, use array_sum() on array_column*)'s 1d returned array.

Code: (Demo)

var_export(
    array_sum(
        array_column(
            array_merge(...$array),
            'NILAI_ANGGARAN'
        )
    )
);
// 26000000

If your actual intent is to accumulate totals for ALL columns of the deepest level, then flatten the array (as described above), then make iterated calls of array_sum() on each column of data.

Code: (Demo)

var_export(
    array_map(
        fn(...$col) => array_sum($col),
        ...array_merge(...$array)
    )
);

array (
  0 => 26000000,
  1 => 2500000,
  2 => 0,
  3 => 0,
  4 => 0,
  5 => 500000,
  6 => 450000,
  7 => 300000,
  8 => 0,
  9 => 0,
  10 => 26250000,
)

To preserve the column names in the result, it will probably be simplest to use nest loops, use the column names as associative keys in the result and ensure that there is always a values to add the current iteration's value to.

Code: (Demo)

$result = [];
foreach ($array as $set) {
    foreach ($set as $row) {
        foreach ($row as $k => $v) {
            $result[$k] = ($result[$k] ?? 0) + $v;
        }
    }
}
var_export($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,
)

Upvotes: 0

Krunal Patel
Krunal Patel

Reputation: 59

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% :)

Upvotes: 0

Uttam Kumar Roy
Uttam Kumar Roy

Reputation: 2058

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

Upvotes: 1

Chetan Ameta
Chetan Ameta

Reputation: 7896

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

Upvotes: 0

aslawin
aslawin

Reputation: 1981

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

Upvotes: 0

Indrasis Datta
Indrasis Datta

Reputation: 8606

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

Upvotes: 0

Related Questions