Some key in the PHP two-dimensional array merge the same parts and different parts to form a new array.

how to change the $arr array into the structure of $newArr

$arr = [
    [
        "id" => "1",
        "name" => "a",
        "attr" => "",
        "val" => "10"
    ],
    [
        "id" => "1",
        "name" => "a",
        "attr" => "",
        "val" => "11"
    ],
    [
        "id" => "2",
        "name" => "b",
        "attr" => "",
        "val" => "12"
    ],
    [
        "id" => "2",
        "name" => "b",
        "attr" => "",
        "val" => "13"
    ],
];

Array structure to be changed:

$arr = [
    [
        "id" => "1",
        "name" => "a",
        "info" => [
             ["attr" => "", "val" => "10"]
             ["attr" => "", "val" => "11"]
        ]
    ],
    [
        "id" => "2",
        "name" => "b",
        "info" => [
             ["attr" => "", "val" => "12"]
             ["attr" => "", "val" => "13"]
        ]
    ]
];
Php
Mar.05,2021

$data = [];
array_map(function($key) use (&$data){
    if (!isset($data[$key['id']])) {
        $data[$key['id']] = [
            'id' => $key['id'],
            'name' => $key['name'],
        ];
    }
    $data[$key['id']]['info'][] = [
        'attr' => $key['attr'],
        'val' => $key['val'],
    ];
}, $arr);
print_r(array_values($data));
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b31be3-2bd9f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b31be3-2bd9f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?