Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?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']] = [];
array_push($res[$el['category']], $el);
return $res;
},
[]
);
print_r($groupped);