$data = [ ['id' => 3, 'name' => 'Alice', 'score' => 85], ['id' => 1, 'name' => 'Charlie', 'score' => 92], ['id' => 2, 'name' => 'Bob', 'score' => 78], ]; // Sort by 'score' in descending order usort($data, function($a, $b) { return $b['score'] <=> $a['score']; // spaceship operator for comparison }); echo "Sorted by score (descending): "; print_r($data); $data2 = [ ['id' => 3, 'name' => 'Alice', 'score' => 85], ['id' => 1, 'name' => 'Charlie', 'score' => 92], ['id' => 2, 'name' => 'Bob', 'score' => 78], ]; // To sort by 'name' in ascending order: usort($data2, function($a, $b) { return $a['name'] <=> $b['name']; }); echo " Sorted by name (ascending): "; print_r($data2);