PHP Array Sort

Summary: in this tutorial, you’ll learn how use the PHP array sort() function to sort elements of an array in ascending order.

Introduction to the PHP array sort() function

The sort() function sorts the elements of an array in place in ascending order. The following shows the syntax of the sort() function:

sort(array &$array, int $flags = SORT_REGULAR): boolCode language: PHP (php)

The sort() function has two parameters:

  • $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.

The $flags parameter defaults to SORT_REGULAR. It means that the function will compare elements of the input array using comparison operators.

To combine multiple flags, you use the | character, for example, SORT_STRING | SORT_FLAG_CASE. The sort() function returns true on success or false on failure.

PHP array sort function examples

Let’s take some examples of using the sort() function.

1) Using the PHP sort() function to sort an array of numbers

The following example uses the PHP sort() function to sort an array of three numbers:

<?php

$numbers = [2, 1, 3];
sort($numbers);

print_r($numbers);Code language: PHP (php)

Output:

Array       
(
    [0] => 1
    [1] => 2
    [2] => 3
)Code language: PHP (php)

This example uses the SORT_REGULAR flag.

2) Using the PHP sort() function to sort an array of strings

The following example uses the sort() function to sort an array of strings alphabetically:

<?php

$names = ['Bob', 'John', 'Alice'];
sort($names, SORT_STRING);

print_r($names);Code language: PHP (php)

Output:

Array
(
    [0] => Alice
    [1] => Bob
    [2] => John
)Code language: PHP (php)

This example uses the SORT_STRING flag that compares array elements as strings.

3) Using the PHP sort() function to sort an array of strings case-insensitively

The following example uses the sort() function to sort an array of strings:

<?php

$fruits = ['apple', 'Banana', 'orange'];
sort($fruits);

print_r($fruits);Code language: PHP (php)

Output:

Array
(
    [0] => Banana
    [1] => apple
    [2] => orange
)    Code language: PHP (php)

To sort an array of strings case-insensitively, you combine the SORT_STRING flag with the SORT_FLAG_CASE flag like this:

<?php

$fruits = ['apple', 'Banana', 'orange'];
sort($fruits, SORT_FLAG_CASE | SORT_STRING);

print($fruits);Code language: PHP (php)

Output:

Array
(
    [0] => apple
    [1] => Banana
    [2] => orange
)    Code language: PHP (php)

4) Using the PHP sort() function to sort an array of strings using “natural ordering”

To sort an array of strings in the “natural ordering”, you combine the SORT_STRING and SORT_NATURAL flags. For example:

<?php

$ranks = ['A-1', 'A-2', 'A-12', 'A-11'];
sort($ranks, SORT_STRING | SORT_NATURAL);

print_r($ranks);Code language: PHP (php)

Output:

Array
(
    [0] => A-1 
    [1] => A-2 
    [2] => A-11
    [3] => A-12
)Code language: PHP (php)

PHP rsort() function

The rsort() function is like the sort() function except that it sorts the elements of an array in descending order. The syntax of the rsort() function is as follows:

rsort(array &$array, int $flags = SORT_REGULAR): boolCode language: PHP (php)

For example, the following sorts the $ranks array’s elements using the natural ordering in the descending order.

<?php

$ranks = ['A-1', 'A-2', 'A-12', 'A-11'];
rsort($ranks, SORT_STRING | SORT_NATURAL);

print_r($ranks);Code language: PHP (php)

Output:

Array
(
    [0] => A-12
    [1] => A-11
    [2] => A-2 
    [3] => A-1 
)Code language: PHP (php)

Summary

  • Use the sort() function to sort elements of an array in ascending order.
  • Use the rsort() function to sort elements of an array in descending order.
  • Use one or more flags to change the sorting behavior of the sort() or rsort() functions.
Did you find this tutorial useful?