doushuhuai7247
doushuhuai7247
2018-12-17 07:01

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 dongshanxiao7328 3年前

    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,
          ),
        ),
      ),
    )
    
    点赞 评论 复制链接分享