3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ ['level_id' => 1, 'level' => 'Trivial', 'description' => 'Foobar'], ['level_id' => 2, 'level' => 'Moderate', 'description' => 'Foobar'], ['level_id' => 3, 'level' => 'Challenging', 'description' => 'Foobar'], ['level_id' => 4, 'level' => 'Formidable'], ]; function toLookup(array $array): array { foreach ($array as $row) { $result[array_shift($row)] = count($row) === 1 ? current($row) : $row; } return $result ?? []; } var_export(toLookup($array));
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
array ( 1 => array ( 'level' => 'Trivial', 'description' => 'Foobar', ), 2 => array ( 'level' => 'Moderate', 'description' => 'Foobar', ), 3 => array ( 'level' => 'Challenging', 'description' => 'Foobar', ), 4 => 'Formidable', )

preferences:
105.89 ms | 407 KiB | 5 Q