3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ "elem-1" => ["title" => "1", "desc" => ""], "elem-2" => ["title" => "2", "desc" => ""], "elem-3" => ["title" => "3", "desc" => ""], "elem-4" => ["title" => "4", "desc" => ""], ]; $result = []; array_walk( $arr, function($row, $key) use(&$result) { $result[] = [$key => $row['title']]; } ); var_export($result); echo "\n---\n"; $result = []; foreach ($arr as $key => $row) { $result[] = [$key => $row['title']]; } var_export($result);
Output for 7.4.0 - 7.4.32, 8.0.1 - 8.0.24, 8.1.0 - 8.1.11
array ( 0 => array ( 'elem-1' => '1', ), 1 => array ( 'elem-2' => '2', ), 2 => array ( 'elem-3' => '3', ), 3 => array ( 'elem-4' => '4', ), ) --- array ( 0 => array ( 'elem-1' => '1', ), 1 => array ( 'elem-2' => '2', ), 2 => array ( 'elem-3' => '3', ), 3 => array ( 'elem-4' => '4', ), )

preferences:
52.86 ms | 418 KiB | 14 Q