首页 > 其他分享 >根据数组按照时间月份分组

根据数组按照时间月份分组

时间:2023-04-23 17:32:54浏览次数:38  
标签:01 billdtimes month 分组 数组 2023 years id 按照

{
    "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);
        }
















标签:01,billdtimes,month,分组,数组,2023,years,id,按照
From: https://blog.51cto.com/yszr/6218439

相关文章

  • PHP 常用数组函数汇集,详细解释描述
    PHPArray函数函数描述PHParray()创建数组。3array_change_key_case()返回其键均为大写或小写的数组。4array_chunk()把一个数组分割为新的数组块。4array_combine()通过合并两个数组来创建一个新数组。5array_count_values()用于统计数组中所有值出现的次数。4array_diff()返回两......
  • Leetcode 53. 最大子数组和 Python题解
    来源:力扣(LeetCode)链接:https://leetcode.cn/problems/maximum-subarray著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。1.动态规划解题思路:对于当前元素nums[i]来说,最大的连续子数组可以为:nums[0:i]中的最大连续子数组加上nums[i]nums[i],此时nums[......
  • 把数组排成最小的数
    classSolution{public:staticboolcmp(inta,intb){stringas=to_string(a),bs=to_string(b);returnas+bs<bs+as;}stringprintMinNumber(vector<int>&nums){sort(nums.begin(),nums.end(),cmp);......
  • 二维数组根据年份跟月份分组成为新数组
    要实现的效果:Array([2023---年份]=>Array([1--月份:]=>Array([0]=>Array([id]=>28[billdtimes]=>......
  • C语言 合并两个升序的数组,成升序的数组
    #include<stdio.h>//两路合并法把两个已按升序排列的数组合并成一个升序数组main(){inta[3]={10,13,15};intb[5]={2,4,6,7,8};intc[10],i=0,j=0,k=0;while(i<3&&j<5)if(a[i]>b[j]){c[k]=b[j];k++;j++;}else{......
  • oracle按身份证号分组后按更新时间排序,取第一条数据
    select  t.*     from (select a.*,row_number() over(PARTITION BY A.IDENTITYCARD order by A.ACCESSIONTIME desc)rn             from T_PATIENT_INFO  a)t    where t.rn= 1 T_PATIENT_INFO--表IDENTITYCARD--证件号码ACC......
  • 八、旋转数组
    给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。示例1:输入:[1,2,3,4,5,6,7]和k=3输出:[5,6,7,1,2,3,4]解释:向右旋转1步:[7,1,2,3,4,5,6]向右旋转2步:[6,7,1,2,3,4,5]向右旋转3步:[5,6,7,1,2,3,4]示例 2:输入:[-1,-100,3,99]和k......
  • 九、数组中存在重复
    给定一个整数数组,判断是否存在重复元素。如果任何值在数组中出现至少两次,函数返回true。如果数组中每个元素都不相同,则返回false。示例1:输入:[1,2,3,1]输出:true示例2:输入:[1,2,3,4]输出:false示例 3:输入:[1,1,1,3,3,4,3,2,4,2]输出:trueclassSolution{pub......
  • java使用数组实现队列
    1.1. 队列的数据结构队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。1.2. Java实现QueueTestpackagech04;publicclassQ......
  • java用数组实现栈
    1.1. 栈的数据结构栈是一种先进后出的数据结果,只能在一端(称为栈顶(top))对数据项进行插入和删除。1.2. Java实现StackTestpackagech04;publicclassStackTest{publicstaticvoidmain(String[]args){ArrayStackstack=newArrayStack(10);......