3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php error_reporting(E_ALL); $db_results = array( array('cat' => 1, 'id' => 1), array('cat' => 1, 'id' => 2), array('cat' => 2, 'id' => 3), array('cat' => 2, 'id' => 4) ); $category_map = array(); foreach ( $db_results as $row ) { $category_map[ $row['cat'] ][ $row['id'] ] = $row; } print_r($category_map);
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Array ( [1] => Array ( [1] => Array ( [cat] => 1 [id] => 1 ) [2] => Array ( [cat] => 1 [id] => 2 ) ) [2] => Array ( [3] => Array ( [cat] => 2 [id] => 3 ) [4] => Array ( [cat] => 2 [id] => 4 ) ) )