How to Sort Arrays in PHP

In this lesson we look at how to sort arrays (including associative arrays) in PHP

SORTING IN ASCENDING ORDER

We simply use the sort() function as follows:

<?php

$names = ['Amy', 'Fred', 'Carl', 'Peter', 'John', 'James', 'Sharon', 'Keith'];

sort($names);

echo '<pre>';

print_r($names);

Line 3: this array contains some names

Line 5: we sort the array called $names

Line 7: we echo a preformat tag so that the array is easy to read

Line 9: we print out the array

The output looks like this:

Array
(
    [0] => Amy
    [1] => Carl
    [2] => Fred
    [3] => James
    [4] => John
    [5] => Keith
    [6] => Peter
    [7] => Sharon
)

SORTING IN DESCENDING ORDER

We use the rsort() function as follows:

<?php

$names = ['Amy', 'Fred', 'Carl', 'Peter', 'John', 'James', 'Sharon', 'Keith'];

rsort($names);

echo '<pre>';

print_r($names);

The output looks like this:

Array
(
    [0] => Sharon
    [1] => Peter
    [2] => Keith
    [3] => John
    [4] => James
    [5] => Fred
    [6] => Carl
    [7] => Amy
)

SORTING AN ASSOCIATIVE ARRAY IN ASCENDING ORDER BY VALUE

Consider the following associative array and the asort() function:

<?php

$names = ['Amy'=>12, 'Fred'=>25, 'Carl'=>43, 'Peter'=>55, 'John'=>42, 'James'=>19, 'Sharon'=>44, 'Keith'=>87];

asort($names);

echo '<pre>';

print_r($names);

The output looks like this:

Array
(
    [Amy] => 12
    [James] => 19
    [Fred] => 25
    [John] => 42
    [Carl] => 43
    [Sharon] => 44
    [Peter] => 55
    [Keith] => 87
)

SORTING AN ASSOCIATIVE ARRAY IN ASCENDING ORDER BY KEY

We use the ksort() function:

<?php

$names = ['Amy'=>12, 'Fred'=>25, 'Carl'=>43, 'Peter'=>55, 'John'=>42, 'James'=>19, 'Sharon'=>44, 'Keith'=>87];

ksort($names);

echo '<pre>';

print_r($names);

The output looks like this:

Array
(
    [Amy] => 12
    [Carl] => 43
    [Fred] => 25
    [James] => 19
    [John] => 42
    [Keith] => 87
    [Peter] => 55
    [Sharon] => 44
)

SORTING AN ASSOCIATIVE ARRAY IN DESCENDING ORDER BY VALUE

We use the arsort() function:

<?php

$names = ['Amy'=>12, 'Fred'=>25, 'Carl'=>43, 'Peter'=>55, 'John'=>42, 'James'=>19, 'Sharon'=>44, 'Keith'=>87];

arsort($names);

echo '<pre>';

print_r($names);

The output looks like this:

Array
(
    [Keith] => 87
    [Peter] => 55
    [Sharon] => 44
    [Carl] => 43
    [John] => 42
    [Fred] => 25
    [James] => 19
    [Amy] => 12
)

SORTING AN ASSOCIATIVE ARRAY IN DESCENDING ORDER BY KEY

We use the krsort() function:

<?php

$names = ['Amy'=>12, 'Fred'=>25, 'Carl'=>43, 'Peter'=>55, 'John'=>42, 'James'=>19, 'Sharon'=>44, 'Keith'=>87];

krsort($names);

echo '<pre>';

print_r($names);

The output looks like this:

Array
(
    [Sharon] => 44
    [Peter] => 55
    [Keith] => 87
    [John] => 42
    [James] => 19
    [Fred] => 25
    [Carl] => 43
    [Amy] => 12
)

Want to Learn More?

Become a member and get access to the member only area of this site.

LEARN MORE

Comments

Your email address will not be published.

PHP for Beginners

Would you like lifetime access to the hugely successful PHP for Beginners course? Click here to find out how.

FREE Code Snippets

FREE hints and tips delivered direct to you once per month.