I just came up with the most brutal hack to use array_reduce with short functions ... heh.

Since "add to the array" and "return the array" are two operations, I turned them into a single one by using a ternary:

```php
array_reduce(
$items,
fn ($carry, $item) =>
($carry[$item['category']][] = $item)
? $carry
: $carry,
[]
);
```

Follow

Maybe there's a way to do this same thing with array_* functions, but nothing came to mind.

Sign in to participate in the conversation
PHP Community on Mastodon

Open source. Open community. We are dedicated to building and enriching the PHP community.