สำหรับสิ่งนี้ ให้ใช้ foreach loop ร่วมกับ [] และใช้เมธอด arary_values()
ตัวอย่าง
<!DOCTYPE html> <html> <body> <?php $details = [ [ "id" => "10001", "firstName" => "John", "lastName" => "Doe" ], [ "id" => "10002", "firstName" => "David", "lastName" => "Miller" ] ]; foreach ($details as $k=> $v) { $someDetails[$v['id']][] = ['LASTNAME' => $v['lastName'], 'FIRSTNAME'=> $v['firstName']]; } print_r(array_values($someDetails)); ?> </body> </html>
ผลลัพธ์
Array ( [0] => Array ( [0] => Array ( [LASTNAME] => Doe [FIRSTNAME] => John ) ) [1] => Array ( [0] => Array ( [LASTNAME] => Miller [FIRSTNAME] => David ) ) )