首页 > 其他分享 >二维数组根据年份跟月份分组成为新数组

二维数组根据年份跟月份分组成为新数组

时间:2023-04-23 15:39:02浏览次数:32  
标签:billEndtime month 二维 分组 数组 Array years billStartime id

要实现的效果:

Array
(
    [2023---年份] => Array
        (
            [1--月份:] => Array
                (
                    [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
                (
                    [0] => 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
                (
                    [0] => 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
                (
                    [0] => 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
                        )

                    [1] => 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
                        )

                    [2] => 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
                        )

                )

        )

    [2022] => Array
        (
            [12] => Array
                (
                    [0] => 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
                        )

                )

        )

)

select代码数据

Array
(
    [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
        )

)


实现代码:

sonlist   查询的元数据  使用&引用方法实现

// 创建基于主键的数组引用
            $refer = array();
            foreach ($sonlist as $key => $data) {
                $refer[$data["years"]][$data["month"]][] = &$sonlist[$key];
            }



 public function getFormatMenus($map = [], $field = '*')
    {
        $list = $this->field($field)->where($map)->select()->order('sort asc')->toArray();
        return $list ? $list : [];
    }

    public function listToTree(array $list)
    {
        $tree  = array();
        $pk    = 'id';
        $pid   = 'pid';
        $root  = 0;
        $child = 'children';
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] = &$list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[] = &$list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent             = &$refer[$parentId];
                        $parent['childs'][] = $data['id'];
                        $parent[$child][]   = &$list[$key];
                    }
                }
            }
        }
        return $tree;
    }

标签:billEndtime,month,二维,分组,数组,Array,years,billStartime,id
From: https://blog.51cto.com/yszr/6217688

相关文章

  • 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);......
  • C语言--数组1
    一维数组和二维数组【一维数组的创建和初始化】数组的创建数组是一组相同类型元素的集合。数组的初始化数组在创建时不指定数组的大小就要初始化。数组的元素个数根据初始化内容来确定。但对于下面代码要区分,其在内存中如何分配。chararr1[]="abc";chararr2[3]={'a','b','c'......
  • 数组求和累加
    publicclassTest2{publicstaticvoidmain(String[]args){int[]arr={68,27,95,88,171,996,51,210};intsum=getEvenNumberSum(arr);System.out.println(sum);}/*一个数组,元素是{68,27,95,88,171,996,51,210}求出该数......
  • #yyds干货盘点# LeetCode程序员面试金典:搜索旋转排序数组
    题目:整数数组nums按升序排列,数组中的值互不相同。在传递给函数之前,nums在预先未知的某个下标k(0<=k<nums.length)上进行了旋转,使数组变为[nums[k],nums[k+1],...,nums[n-1],nums[0],nums[1],...,nums[k-1]](下标从0开始计数)。例如,[0,1,2,4,5,6,7]在下标3处......
  • 2022-04-22:给你两个正整数数组 nums 和 target ,两个数组长度相等。 在一次操作中,你可
    2022-04-22:给你两个正整数数组nums和target,两个数组长度相等。在一次操作中,你可以选择两个不同的下标i和j,其中0<=i,j<nums.length,并且:令nums[i]=nums[i]+2且令nums[j]=nums[j]-2。如果两个数组中每个元素出现的频率相等,我们称两个数组是相似的......