On 2026-03-24 23:08, 沉默领域、 wrote:
// Proposed
$names = array_pluck($users, 'name');
$names = array_where($users, fn($u) => $u->active);
$first = array_first($users);
$grouped = array_group_by($users, 'role');
$compact = array_compact($data);
Of those, array_compact() is obscure in its purpose, and of the others
one exists under the suggested name and three of the others exist under
different names.
array_group_by() is the only one that isn't already implemented. I think
it means:
$roles = array_unique(array_column($users, 'role'));
$grouped = array_combine($roles, array_map(fn($r) =>
array_filter($users, fn($u) => $u['role']==$r), $roles));
though there are more efficient (and readable) ways of doing that.
--
Optimising the implementation would be more difficult if the language
were allowed to be sloppier. Conversely, tightening up the language
allows improved optimisation.