phparraysmultidimensional-arraysumarray-sum

How to get sum of array values according to some index (non-sorted)


I want the sum of monthly_rental and arc if the month and full_name are the same in one array.

Input Array

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 521
                    [full_name] => Nilesh chauhan
                    [monthly_rental] => 15000
                    [arc] => 180000
                    [month] => 1
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 1549
                    [full_name] => Ritesh shah
                    [monthly_rental] => 14166.666666667
                    [arc] => 170000
                    [month] => 2
                )

            [1] => Array
                (
                    [id] => 589
                    [full_name] => Gajanan satpute
                    [monthly_rental] => 14166.666666667
                    [arc] => 170000
                    [month] => 2
                )

            [2] => Array
                (
                    [id] => 521
                    [full_name] => Nilesh chauhan
                    [monthly_rental] => 13333.333333333
                    [arc] => 160000
                    [month] => 2
                )

            [3] => Array
                (
                    [id] => 1549
                    [full_name] => Ritesh shah
                    [monthly_rental] => 11250
                    [arc] => 135000
                    [month] => 2
                )

            [4] => Array
                (
                    [id] => 521
                    [full_name] => Nilesh chauhan
                    [monthly_rental] => 7500
                    [arc] => 90000
                    [month] => 2
                )

            [5] => Array
                (
                    [id] => 1549
                    [full_name] => Ritesh shah
                    [monthly_rental] => 7000
                    [arc] => 84000
                    [month] => 2
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [id] => 1549
                    [full_name] => Ritesh shah
                    [monthly_rental] => 35833.333333333
                    [arc] => 430000
                    [month] => 3
                )

            [1] => Array
                (
                    [id] => 1549
                    [full_name] => Ritesh shah
                    [monthly_rental] => 14166.666666667
                    [arc] => 170000
                    [month] => 3
                )

            [2] => Array
                (
                    [id] => 521
                    [full_name] => Nilesh chauhan
                    [monthly_rental] => 13333.333333333
                    [arc] => 160000
                    [month] => 3
                )

            [3] => Array
                (
                    [id] => 521
                    [full_name] => Nilesh chauhan
                    [monthly_rental] => 8750
                    [arc] => 105000
                    [month] => 3
                )

            [4] => Array
                (
                    [id] => 589
                    [full_name] => Gajanan satpute
                    [monthly_rental] => 7500
                    [arc] => 90000
                    [month] => 3
                )

            [5] => Array
                (
                    [id] => 1549
                    [full_name] => Ritesh shah
                    [monthly_rental] => 6493.3333333333
                    [arc] => 77920
                    [month] => 3
                )
        )
)

There are 6 sub-arrays in 1st and 2-nd indexes, I want the sum of 0,1,5 (Ritesh Shah, Month 2) array and make them unique. Same as this I have a long list of the array but I will manage them all if any of you just suggest to me how to get sum value of this array.

OUTPUT That I want :

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 521
                    [full_name] => Nilesh chauhan
                    [monthly_rental] => 15000
                    [arc] => 180000
                    [month] => 1
                )

        )
        [1] => Array
        (
            [0] => Array
                (
                    [id] => 1549
                    [full_name] => Ritesh shah
                    [monthly_rental] => 32416.66
                    [arc] => 389000
                    [month] => 2
                )

            [1] => Array
                (
                    [id] => 589
                    [full_name] => Gajanan satpute
                    [monthly_rental] => 14166.666666667
                    [arc] => 170000
                    [month] => 2
                )

            [2] => Array
                (
                    [id] => 521
                    [full_name] => Nilesh chauhan
                    [monthly_rental] => 20833.33
                    [arc] => 250000
                    [month] => 2
                )

        )
...

Solution

  • You can apply this function and retrieve your expected output:

    function getCustomMonoRecords($data){
    
        $res = [];
    
        foreach($data as $m=>$ar){
            $res[$m] = [];   
            $tmp = []; 
            $i = 0;
            foreach($ar as $ind=>$rec){
                if(!array_key_exists($rec['id'],$tmp)) {
                    $tmp[$rec['id']] = $i;  
                    $res[$m][$i] = $rec;
                    $i++;
                } else {
                    $in = $tmp[$rec['id']];
                    $res[$m][$in]['monthly_rental'] += $rec['monthly_rental'];
                    $res[$m][$in]['arc'] += $rec['arc'];
                }
            } 
        }
        return $res;
    }
    

    Demo

    Or a little bit different:

    function getCustomMonoRecords2($data){
    
        $res = [];
    
        foreach($data as $m=>$ar){
            $res[$m] = [];     
            foreach($ar as $ind=>$rec){ 
                if(!isset($res[$m][$rec['id']])){
                    $res[$m][$rec['id']] = $rec;
                } else {
                    $res[$m][$rec['id']]['monthly_rental'] += $rec['monthly_rental'];
                    $res[$m][$rec['id']]['arc'] += $rec['arc'];
                } 
            } 
            $res[$m] = array_values($res[$m]);
        } 
        return $res;
    }
    

    Demo2