sort()
Syntax
sort( array &$array, int $flags = SORT_REGULAR): bool
- $array is the input array to sort.
- $flags argument is one or a combination of multiple flags that change the sorting behavior of the function.
Example
1. Using the PHP sort() function to sort an array of numbers
1 2 3 4 5 6 | <?php $numbers = [2, 1, 3]; sort($numbers); print_r($numbers); |
Output:
1 2 3 4 5 6 | Array ( [0] => 1 [1] => 2 [2] => 3 ) |
2.Using the PHP sort() function to sort an array of strings
1 2 3 4 5 6 | <?php $names = ['Bob', 'John', 'Alice']; sort($names, SORT_STRING); print_r($names); |
Output:
1 2 3 4 5 6 | Array ( [0] => Alice [1] => Bob [2] => John ) |
3.Using the PHP sort() function to sort an array of strings case-insensitively
1 2 3 4 5 6 | <?php $fruits = ['apple', 'Banana', 'orange']; sort($fruits); print_r($fruits); |
Output:
1 2 3 4 5 6 | Array ( [0] => Banana [1] => apple [2] => orange ) |
4.Using the PHP sort() function to sort an array of strings using “natural ordering”
1 2 3 4 5 6 | <?php $ranks = ['A-1', 'A-2', 'A-12', 'A-11']; sort($ranks, SORT_STRING | SORT_NATURAL); print_r($ranks); |
Output:
1 2 3 4 5 6 7 | Array ( [0] => A-1 [1] => A-2 [2] => A-11 [3] => A-12 ) |
rsort()
Syntax
rsort( array &$array, int $flags = SORT_REGULAR): bool
Example
1 2 3 4 5 6 | <?php $ranks = ['A-1', 'A-2', 'A-12', 'A-11']; rsort($ranks, SORT_STRING | SORT_NATURAL); print_r($ranks); |
Output:
1 2 3 4 5 6 7 | Array ( [0] => A-12 [1] => A-11 [2] => A-2 [3] => A-1 ) |
uasort()
Syntax
uasort( array &$array, callable $callback): bool
- $array is the input array.
- $callback is the user-defined comparison function.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php $countries = [ 'China' => ['gdp' => 12.238 , 'gdp_growth' => 6.9], 'Germany' => ['gdp' => 3.693 , 'gdp_growth' => 2.22 ], 'Japan' => ['gdp' => 4.872 , 'gdp_growth' => 1.71 ], 'USA' => ['gdp' => 19.485, 'gdp_growth' => 2.27 ], ]; // sort the country by GDP uasort($countries, function ($x, $y) { return $x['gdp'] <=> $y['gdp']; }); // show the array foreach ($countries as $name => $stat) { echo "$name has a GDP of {$stat['gdp']} trillion USD with a GDP growth rate of {$stat['gdp_growth']}%" . '<br>'; } |
Output:
1 2 3 4 | Germany has a GDP of 3.693 trillion USD with a GDP growth rate of 2.22% Japan has a GDP of 4.872 trillion USD with a GDP growth rate of 1.71% China has a GDP of 12.238 trillion USD with a GDP growth rate of 6.9% USA has a GDP of 19.485 trillion USD with a GDP growth rate of 2.27% |
asort()
Syntax
asort( array &$array, int $flags = SORT_REGULAR): bool
- $array is the input array.
- $flags is one or more flags that change the sorting behavior.
Example
1 2 3 4 5 6 7 8 9 10 11 | <?php $mountains = [ 'K2' => 8611, 'Lhotse' => 8516, 'Mount Everest' => 8848, 'Kangchenjunga' => 8586, ]; asort($mountains); print_r($mountains); |
Output:
1 2 3 4 5 6 7 | Array ( [Lhotse] => 8516 [Kangchenjunga] => 8586 [K2] => 8611 [Mount Everest] => 8848 ) |
arsort()
Syntax
arsort( array &$array, int $flags = SORT_REGULAR): bool
Example
1 2 3 4 5 6 7 8 9 10 11 | <?php $mountains = [ 'K2' => 8611, 'Lhotse' => 8516, 'Mount Everest' => 8848, 'Kangchenjunga' => 8586, ]; arsort($mountains); print_r($mountains); |
Output:
1 2 3 4 5 6 7 | Array ( [Mount Everest] => 8848 [K2] => 8611 [Kangchenjunga] => 8586 [Lhotse] => 8516 ) |
usort()
Syntax
usort( array &$array, callable $callback): bool
- $array is the input array.
- $callback is the custom comparison function.
Example
1. Using the PHP usort() function to sort an array of numbers
1 2 3 4 5 6 7 8 9 10 11 12 | <?php $numbers = [2, 1, 3]; usort($numbers, function ($x, $y) { if ($x === $y) { return 0; } return $x < $y ? -1 : 1; }); print_r($numbers); |
Output:
1 2 3 4 5 6 | Array ( [0] => 1 [1] => 2 [2] => 3 ) |
2.Using the PHP usort() function to sort an array of strings by length
1 2 3 4 5 6 | <?php $names = [ 'Alex', 'Peter', 'John' ]; usort($names, fn($x,$y) => strlen($x) <=> strlen($y)); var_dump($names); |
Output:
1 2 3 4 5 6 | Array(3) { [0]=> string(4) "Alex" [1]=> string(4) "John" [2]=> string(5) "Peter" } |
3.Using the PHP usort() function to sort an array of objects
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php class Person { public $name; public $age; public function __construct(string $name, int $age) { $this->name = $name; $this->age = $age; } } $group = [ new Person('Bob', 20), new Person('Alex', 25), new Person('Peter', 30), ]; usort($group, fn($x, $y) => $x->age <=> $y->age); print_r($group); |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Array ( [0] => Person Object ( [name] => Bob [age] => 20 ) [1] => Person Object ( [name] => Alex [age] => 25 ) [2] => Person Object ( [name] => Peter [age] => 30 ) ) |
ksort()
Syntax
ksort( array &$array, int $flags = SORT_REGULAR): bool
- $array is the input array
- $flags changes the sorting behavior using one or more values SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING, SORT_NATURAL, and SORT_FLAG_CASE.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php $employees = [ 'john' => [ 'age' => 24, 'title' => 'Front-end Developer' ], 'alice' => [ 'age' => 28, 'title' => 'Web Designer' ], 'bob' => [ 'age' => 25, 'title' => 'MySQL DBA' ] ]; ksort($employees, SORT_STRING); print_r($employees); |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Array ( [alice] => Array ( [age] => 28 [title] => Web Designer ) [bob] => Array ( [age] => 25 [title] => MySQL DBA ) [john] => Array ( [age] => 24 [title] => Front-end Developer ) ) |
krsort()
Syntax
krsort( array &$array, int $flags = SORT_REGULAR): bool
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php $employees = [ 'john' => [ 'age' => 24, 'title' => 'Front-end Developer' ], 'alice' => [ 'age' => 28, 'title' => 'Web Designer' ], 'bob' => [ 'age' => 25, 'title' => 'MySQL DBA' ] ]; krsort($employees, SORT_STRING); print_r($employees); |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Array ( [john] => Array ( [age] => 24 [title] => Front-end Developer ) [bob] => Array ( [age] => 25 [title] => MySQL DBA ) [alice] => Array ( [age] => 28 [title] => Web Designer ) ) |
uksort()
Syntax
uksort( array &$array, callable $callback): bool
- $array is the input array.
- $callback is the comparison function that determines the order of keys.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php $names = [ 'c' => 'Charlie', 'A' => 'Alex', 'b' => 'Bob' ]; uksort( $names, fn ($x, $y) => strtolower($x) <=> strtolower($y) ); print_r($names); |
Output:
1 2 3 4 5 6 | Array ( [A] => Alex [b] => Bob [c] => Charlie ) |