3v4l.org

run code in 300+ PHP versions simultaneously
<?php $products = array( [ 'id' => 12, 'group' => 'abc', 'name' => 'Lorem', ], [ 'id' => 12, 'group' => 'def', 'name' => 'Ipsum', ], [ 'id' => 34, 'group' => 'ghi', 'name' => 'Dolor', ], [ 'id' => 34, 'group' => 'jkl', 'name' => 'Sit', ], [ 'id' => 34, 'group' => 'mno', 'name' => 'Amet', ], ); $grouped = array_reduce($products, function ($c, $v) { $c[$v['id']][$v['group']] = $v; return $c; }, []); print_r($grouped); $products[] = [ 'id' => 34, 'group' => 'jkl', 'name' => 'Consectetur', ]; $grouped = array_reduce($products, function ($c, $v) { $c[$v['id']][$v['group']] = $v; return $c; }, []); print_r($grouped); $grouped = array_reduce($products, function ($c, $v) { $c[$v['id']][$v['group']][] = $v; return $c; }, []); print_r($grouped);

This is an error 500

Value for `_results` contains invalid data `array`


preferences:
170.23 ms | 3077 KiB | 12 Q