{
    "code": 200,
    "msg": "success",
    "data": {
        "sumprice": 42,
        "arrearslist": [
            {
                "years": 2023,
                "month": 1,
                "sumprice": 11,
                "childen": [
                    {
                        "id": 28,
                        "billdtimes": "2023-01-01",
                        "month": 1,
                        "price": "8.00",
                        "years": 2023,
                        "wuyeId": 1,
                        "communityId": 3,
                        "unitNumber": 2,
                        "buildingId": 1,
                        "roomNumber": "302",
                        "billStartime": "2023-01-01",
                        "billEndtime": "2023-01-31"
                    },
                    {
                        "id": 31,
                        "billdtimes": "2023-01-01",
                        "month": 1,
                        "price": "3.00",
                        "years": 2023,
                        "wuyeId": 1,
                        "communityId": 3,
                        "unitNumber": 2,
                        "buildingId": 1,
                        "roomNumber": "302",
                        "billStartime": "2023-01-01",
                        "billEndtime": "2023-01-31"
                    }
                ]
            },
            {
                "years": 2023,
                "month": 2,
                "sumprice": "8.00",
                "childen": [
                    {
                        "id": 29,
                        "billdtimes": "2023-02-01",
                        "month": 2,
                        "price": "8.00",
                        "years": 2023,
                        "wuyeId": 1,
                        "communityId": 3,
                        "unitNumber": 2,
                        "buildingId": 1,
                        "roomNumber": "302",
                        "billStartime": "2023-02-01",
                        "billEndtime": "2023-02-28"
                    }
                ]
            },
            {
                "years": 2023,
                "month": 3,
                "sumprice": "8.00",
                "childen": [
                    {
                        "id": 30,
                        "billdtimes": "2023-03-01",
                        "month": 3,
                        "price": "8.00",
                        "years": 2023,
                        "wuyeId": 1,
                        "communityId": 3,
                        "unitNumber": 2,
                        "buildingId": 1,
                        "roomNumber": "302",
                        "billStartime": "2023-03-01",
                        "billEndtime": "2023-03-31"
                    }
                ]
            },
            {
                "years": 2023,
                "month": 4,
                "sumprice": 9,
                "childen": [
                    {
                        "id": 6,
                        "billdtimes": "2023-04-01",
                        "month": 4,
                        "price": "6.00",
                        "years": 2023,
                        "wuyeId": 1,
                        "communityId": 3,
                        "unitNumber": 2,
                        "buildingId": 1,
                        "roomNumber": "302",
                        "billStartime": "1970-01-01",
                        "billEndtime": "1970-01-01"
                    },
                    {
                        "id": 7,
                        "billdtimes": "2023-04-01",
                        "month": 4,
                        "price": "1.00",
                        "years": 2023,
                        "wuyeId": 1,
                        "communityId": 3,
                        "unitNumber": 2,
                        "buildingId": 1,
                        "roomNumber": "302",
                        "billStartime": "1970-01-01",
                        "billEndtime": "1970-01-01"
                    },
                    {
                        "id": 8,
                        "billdtimes": "2023-04-01",
                        "month": 4,
                        "price": "2.00",
                        "years": 2023,
                        "wuyeId": 1,
                        "communityId": 3,
                        "unitNumber": 2,
                        "buildingId": 1,
                        "roomNumber": "302",
                        "billStartime": "1970-01-01",
                        "billEndtime": "1970-01-01"
                    }
                ]
            },
            {
                "years": 2022,
                "month": 12,
                "sumprice": "6.00",
                "childen": [
                    {
                        "id": 11,
                        "billdtimes": "2022-12-01",
                        "month": 12,
                        "price": "6.00",
                        "years": 2022,
                        "wuyeId": 1,
                        "communityId": 3,
                        "unitNumber": 2,
                        "buildingId": 1,
                        "roomNumber": "302",
                        "billStartime": "2022-12-01",
                        "billEndtime": "2022-12-30"
                    }
                ]
            }
        ]
    }
}

元数据:


(
    [0] => Array
        (
            [id] => 28
            [billdtimes] => 1672502400
            [month] => 1
            [price] => 8.00
            [years] => 2023
            [wuyeId] => 1
            [communityId] => 3
            [unitNumber] => 2
            [buildingId] => 1
            [roomNumber] => 302
            [billStartime] => 1672502400
            [billEndtime] => 1675094400
        )

    [1] => Array
        (
            [id] => 31
            [billdtimes] => 1672502400
            [month] => 1
            [price] => 3.00
            [years] => 2023
            [wuyeId] => 1
            [communityId] => 3
            [unitNumber] => 2
            [buildingId] => 1
            [roomNumber] => 302
            [billStartime] => 1672502400
            [billEndtime] => 1675094400
        )

    [2] => Array
        (
            [id] => 29
            [billdtimes] => 1675180800
            [month] => 2
            [price] => 8.00
            [years] => 2023
            [wuyeId] => 1
            [communityId] => 3
            [unitNumber] => 2
            [buildingId] => 1
            [roomNumber] => 302
            [billStartime] => 1675180800
            [billEndtime] => 1677513600
        )

    [3] => Array
        (
            [id] => 30
            [billdtimes] => 1677600000
            [month] => 3
            [price] => 8.00
            [years] => 2023
            [wuyeId] => 1
            [communityId] => 3
            [unitNumber] => 2
            [buildingId] => 1
            [roomNumber] => 302
            [billStartime] => 1677600000
            [billEndtime] => 1680192000
        )

    [4] => Array
        (
            [id] => 6
            [billdtimes] => 1680313031
            [month] => 4
            [price] => 6.00
            [years] => 2023
            [wuyeId] => 1
            [communityId] => 3
            [unitNumber] => 2
            [buildingId] => 1
            [roomNumber] => 302
            [billStartime] => 0
            [billEndtime] => 0
        )

    [5] => Array
        (
            [id] => 7
            [billdtimes] => 1680313031
            [month] => 4
            [price] => 1.00
            [years] => 2023
            [wuyeId] => 1
            [communityId] => 3
            [unitNumber] => 2
            [buildingId] => 1
            [roomNumber] => 302
            [billStartime] => 0
            [billEndtime] => 0
        )

    [6] => Array
        (
            [id] => 8
            [billdtimes] => 1680313031
            [month] => 4
            [price] => 2.00
            [years] => 2023
            [wuyeId] => 1
            [communityId] => 3
            [unitNumber] => 2
            [buildingId] => 1
            [roomNumber] => 302
            [billStartime] => 0
            [billEndtime] => 0
        )

    [7] => Array
        (
            [id] => 11
            [billdtimes] => 1669859617
            [month] => 12
            [price] => 6.00
            [years] => 2022
            [wuyeId] => 1
            [communityId] => 3
            [unitNumber] => 2
            [buildingId] => 1
            [roomNumber] => 302
            [billStartime] => 1669859617
            [billEndtime] => 1672365217
        )

)


 if(!$payStatus){
            $sonlist = $new_son_bill->field("id,wuye_id,community_id,billdtimes,unit_number,building_id,month,price,room_number,years,bill_startime,bill_endtime")
                ->where($wheres)
                ->order(['month','room_number'=>'asc'])
                ->select()->toArray();//查询子表列表
            foreach ($sonlist as &$item) {
                $item['billdtimes'] = date("Y-m-d",$item['billdtimes']);
                $item['billStartime'] = date("Y-m-d",$item['billStartime']);
                $item['billEndtime'] = date("Y-m-d",$item['billEndtime']);
            }
下面是根据年份跟月份提取数组,关键点就是:$keys  将年份月份弄成keys来判断是否设置然后进行设置内容
            pp($sonlist);
            $arr1 = [];
            foreach ($sonlist as $k=>$v) {
                $keys = $v['years'].$v['month'];
                if(!isset($arr1[$keys])){
                    $arr1[$keys]['years'] = $v['years'];
                    $arr1[$keys]['month'] = $v['month'];
                    $arr1[$keys]['sumprice'] = $v['price'];
                    $arr1[$keys]['childen'][] = $v;
                }else{
                    $arr1[$keys]['sumprice'] += $v['price'];
                    $arr1[$keys]['childen'][] = $v;
                }
            }

            $resinfo['arrearslist'] = array_values($arr1);
        }