Field Manual PHP / Laravel reference
← PHP

Filtering and sorting

Filter selects values; sorting changes their order.

What it is

Filter selects values; sorting changes their order.

When to use it

Use for small in-memory lists, not entire large database tables.

Syntax

$active = array_values(array_filter($users, fn ($u) => $u["active"]));
usort($active, fn ($a, $b) => $a["name"] <=> $b["name"]);

Example

$users = [["name" => "Lin", "active" => true], ["name" => "Ada", "active" => true]];
$active = array_values(array_filter($users, fn ($u) => $u["active"]));
usort($active, fn ($a, $b) => strcmp($a["name"], $b["name"]));

Common mistakes

usort mutates and reindexes; array_filter without a callback drops zero and false.

Related topics

Authorization and RBAC