<?php $array = [ [ 'id' => 112, 'name' => 'John', 'country' => 'Spain', 'age' => 24, 'company' => 'One', 'price' => 10 ], [ 'id' => 112, 'name' => 'John', 'country' => 'Spain', 'age' => 24, 'company' => 'Two', 'price' => 15 ], [ 'id' => 112, 'name' => 'John', 'country' => 'Spain', 'age' => 24, 'company' => 'Three', 'price' => 20 ], [ 'id' => 224, 'name' => 'Paul', 'country' => 'France', 'age' => 25, 'company' => 'One', 'price' => 25 ], [ 'id' => 224, 'name' => 'Paul', 'country' => 'France', 'age' => 25, 'company' => 'Two', 'price' => 40 ] ]; $result = []; foreach ($array as $row) { $companies = array_splice($row, -2); $result[$row['id']] ??= $row; $result[$row['id']]['companies'][] = $companies; } var_export($result);
You have javascript disabled. You will not be able to edit any code.