Using usort() for Custom Array Sorting in PHP


Sort an array by values using a user-defined comparison function.

Source Code

$users = [
    ['name' => 'John', 'age' => 30],
    ['name' => 'Jane', 'age' => 25],
    ['name' => 'Doe', 'age' => 22]
];
usort($users, function($a, $b) { return $a['age']  $b['age']; });
print_r($users);
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments