3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $categories = array( 'category_1' => array( 'name' => 'Category 1', 'column' => '1' ), 'category_2' => array( 'name' => 'Category 2', 'column' => '2' ), 'category_3' => array( 'name' => 'Category 2', 'column' => '1' ), 'category_4' => array( 'name' => 'Category 4', 'column' => '3' ), 'category_4' => array( 'name' => 'Category 4', 'column' => '2' ), ); $formatted = array(); foreach ($categories as $item) { $column = $item['column']; if (isset($formatted[$column])) { $formatted[$column][] = $item; } else { $formatted[$column] = array($item); } } var_dump($formatted);
Output for 4.3.0 - 7.2.0
array(2) { [1]=> array(2) { [0]=> array(2) { ["name"]=> string(10) "Category 1" ["column"]=> string(1) "1" } [1]=> array(2) { ["name"]=> string(10) "Category 2" ["column"]=> string(1) "1" } } [2]=> array(2) { [0]=> array(2) { ["name"]=> string(10) "Category 2" ["column"]=> string(1) "2" } [1]=> array(2) { ["name"]=> string(10) "Category 4" ["column"]=> string(1) "2" } } }