doushuhuai7247 2018-12-17 07:01
浏览 162
已采纳

php根据键的值对二维数组进行排序和分组

I have a two-dimensional array that needs to be grouped according to assemble_id ,I can't get the result I want by traversing. Below is the original array:

$list = [
0 =>[
    'nickname' => 'Bob',
    'phone' => 15295892895,
    'is_group' => 1,
    'created_at' => 1544944181,
    'assemble_id' => 'c1d0zUbmP',
    'status' => 1
],
1 =>[
    'nickname' => 'Jack',
    'phone' => 15295892895,
    'is_group' => 1,
    'created_at' => 1544944181,
    'assemble_id' => 'ED6OJX4VV',
    'status' => 1
],
2 =>[
    'nickname' => 'Grace',
    'phone' => 15295892895,
    'is_group' => 0,
    'created_at' => 1544944181,
    'assemble_id' => 'c1d0zUbmP',
    'status' => 1
],
3 =>[
    'nickname' => 'Jelly',
    'phone' => 15295892895,
    'is_group' => 0,
    'created_at' => 1544944181,
    'assemble_id' => 'ED6OJX4VV',
    'status' => 1
]
];

What i need is:

     [
            [
                'assemble_id' => 'c1d0zUbmP',
                'assemble_group' => [
                    [
                        'nickname' => 'Bob',
                        'phone' => 15295892895,
                        'is_group' => 1,
                        'created_at' => 1544944181,
                        'assemble_id' => 'c1d0zUbmP',
                        'status' => 1
                    ],
                    [
                        'nickname' => 'Grace',
                        'phone' => 15295892895,
                        'is_group' => 0,
                        'created_at' => 1544944181,
                        'assemble_id' => 'c1d0zUbmP',
                        'status' => 1
                    ]
                ]
            ],
            [
                'assemble_id' => 'ED6OJX4VV',
                'assemble_group' => [
                    [
                        'nickname' => 'Jack',
                        'phone' => 15295892895,
                        'is_group' => 1,
                        'created_at' => 1544944181,
                        'assemble_id' => 'ED6OJX4VV',
                        'status' => 1
                    ],
                    [
                        'nickname' => 'Jack',
                        'phone' => 15295892895,
                        'is_group' => 0,
                        'created_at' => 1544944181,
                        'assemble_id' => 'ED6OJX4VV',
                        'status' => 1
                    ]
                ]
            ]
        ];

I need to group according to the same assemble_id and put is_group=1 in the first of each group. hery is my code:

function formatData($list)
{
    $data = [];
    foreach ($list as $k=>$v) {
        $list[$k]['created_at'] = formatDate($v['created_at'],'Y-m-d H:i');
        $list[$k]['phone'] = setMobile($v['phone']);
        $data[$k]['assemble_id'] = $v['assemble_id'];
        $data[$v['assemble_id']][] = $list[$k];
    }
    return $data;
}

This did not get the results I wanted.

  • 写回答

1条回答 默认 最新

  • dongshanxiao7328 2018-12-17 07:14
    关注

    Assign temporary associative keys, then remove them when finished looping.

    Code: (Demo)

    foreach ($list as $row) {
        $result[$row['assemble_id']]['assemble_id'] = $row['assemble_id'];
        $result[$row['assemble_id']]['assemble_group'][] = [
            'nickname' => $row['nickname'],
            'phone' => $row['phone'],  // add your function here
            'is_group' => $row['is_group'],
            'created_at' => $row['created_at'],    // add your function here
            'assemble_id' => $row['assemble_id'],
            'status' => $row['status']
        ];
    }
    var_export(array_values($result));
    

    Output:

    array (
      0 => 
      array (
        'assemble_id' => 'c1d0zUbmP',
        'assemble_group' => 
        array (
          0 => 
          array (
            'nickname' => 'Bob',
            'phone' => 15295892895,
            'is_group' => 1,
            'created_at' => 1544944181,
            'assemble_id' => 'c1d0zUbmP',
            'status' => 1,
          ),
          1 => 
          array (
            'nickname' => 'Grace',
            'phone' => 15295892895,
            'is_group' => 0,
            'created_at' => 1544944181,
            'assemble_id' => 'c1d0zUbmP',
            'status' => 1,
          ),
        ),
      ),
      1 => 
      array (
        'assemble_id' => 'ED6OJX4VV',
        'assemble_group' => 
        array (
          0 => 
          array (
            'nickname' => 'Jack',
            'phone' => 15295892895,
            'is_group' => 1,
            'created_at' => 1544944181,
            'assemble_id' => 'ED6OJX4VV',
            'status' => 1,
          ),
          1 => 
          array (
            'nickname' => 'Jelly',
            'phone' => 15295892895,
            'is_group' => 0,
            'created_at' => 1544944181,
            'assemble_id' => 'ED6OJX4VV',
            'status' => 1,
          ),
        ),
      ),
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题