PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php $data = [ ['category' => 'Category1', 'name' => 'Product1'], ['category' => 'Category1', 'name' => 'Product2'], ['category' => 'Category2', 'name' => 'Product3'], ['category' => 'Category3', 'name' => 'Product4'], ['category' => 'Category2', 'name' => 'Product5'], ]; $groupped = array_reduce( $data, function($res, $el) { if (!isset($res[$el['category']])) $res[$el['category']] = []; return $res; }, [] ); print_r($groupped);
Show:  
Copy Clear