3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ ['id' => 1, 'text' => 'text1', 'type' => 'A'], ['id' => 1, 'text' => 'text2', 'type' => 'B'], ['id' => 1, 'text' => 'text3', 'type' => 'C'], ['id' => 2, 'text' => 'text4', 'type' => 'A'], ['id' => 2, 'text' => 'text5', 'type' => 'B'], ['id' => 2, 'text' => 'text6', 'type' => 'C'], ['id' => 3, 'text' => 'text7', 'type' => 'A'], ['id' => 3, 'text' => 'text8', 'type' => 'B'], ['id' => 3, 'text' => 'text9', 'type' => 'C'], ]; $newArr = []; $ids = array_values(array_unique(array_column($arr, 'id'))); foreach ($ids as $key => $id) { foreach ($arr as $value) { if ($value['id'] === $id) { $newArr[$key]['id'] = $id; $newArr[$key][$value['type']] = $value['text']; } } } echo '<pre>'; print_r($newArr); echo '</pre>';
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
<pre>Array ( [0] => Array ( [id] => 1 [A] => text1 [B] => text2 [C] => text3 ) [1] => Array ( [id] => 2 [A] => text4 [B] => text5 [C] => text6 ) [2] => Array ( [id] => 3 [A] => text7 [B] => text8 [C] => text9 ) ) </pre>

preferences:
102.82 ms | 1595 KiB | 4 Q