1

我想要一个数组中的和monthly_rental是否arc相同。monthfull_name

输入数组

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

第一个和第二个索引中有 6 个子数组,我想要0,1,5 (Ritesh Shah, Month 2)数组的总和并使它们唯一。与此相同,我有一个很长的数组列表,但如果你们中的任何人只是向我建议如何获取该数组的总和值,我将管理它们。

我想要的输出:

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
                )

        )
...
4

1 回答 1

0

您可以应用此功能并检索您的预期输出:

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

演示

或者有点不同:

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

演示2

于 2019-12-06T13:06:03.803 回答