Recently I had to refactor a few quite old CRON jobs which made extensive use of arrays within loops.

As usual with old code I had to brush up a little as a reminder so thought I would summarise a few quick notes:

sort() and rsort() are for sorting indexed arrays
asort() and arsort() are for sorting associative arrays
ksort() and krsort() are for sorting associative arrays by key
array_multisort() is for sorting multiple related arrays and multidimensional arrays

The PHP sort() function sorts values contained within an indexed array in ascending order.
This means that A-Z, and 0-9. Uppercase letters come before lowercase letters, and all letters come before numbers.

$testArray = array( 1, 2, 3, ‘a’, ‘b’, ‘c’, ‘A’, ‘B’, ‘C’ );
sort( $testArray );

// Displays “A B C a b c 1 2 3″
foreach ( $testArray as $value ) echo “$value “;

rsort() works exactly the same as sort(), except that it sorts in descending order.

$testArray = array( 1, 2, 3, ‘a’, ‘b’, ‘c’, ‘A’, ‘B’, ‘C’ );
rsort( $testArray );

// Displays “3 2 1 c b a C B A”
foreach ( $testArray as $value ) echo “$value “;

Sorting associative arrays using asort() and arsort()

sort() and rsort() are great for indexed arrays where you usually do not care about the relationship between keys and values; however with an associative array they can cause problems:

$book = array( “title” => “Lord Of The Rings”, “author” => “J.R.R. Tolkien”, “year” => 1954 );
sort( $book );

// Displays “Array ( [0] => J.R.R. Tolkien [1] => Lord Of The Rings [3] => 1954 )”
print_r( $book );

As you can see, the sort() function has reindexed the array with numeric indices and in the process destroyed the original string indices of “title”, “author”, and “year”.

To sort values in an associative array whilst preserving keys, use asort() and arsort() instead as these functions preserve not only the keys, but also the relationship between keys and their values.

$book = array( “title” => “Lord Of The Rings”, “author” => “J.R.R. Tolkien”, “year” => 1954 );
asort( $book );

// Displays “Array ( [author] => J.R.R. Tolkien [title] => Lord Of The Rings [year] => 1954 )”
print_r( $book );

Sorting associative arrays by key using ksort() and krsort()

As well as sorting an associative array by value it can also be sorted by key. ksort() sorts the elements in ascending key order, while krsort() sorts in descending key order.

Just like asort() and arsort() these functions will preserve the relationship between key and value.

$book = array( “title” => “Lord Of The Rings”, “author” => “J.R.R. Tolkien”, “year” => 1954 );
ksort( $book );

// Displays “Array ( [author] => J.R.R. Tolkien [title] => Lord Of The Rings [year] => 1954 )”
print_r( $book );

krsort( $book );
// Displays “Array ( [year] => 1954 [title] => Lord Of The Rings [author] => J.R.R. Tolkien )”
print_r( $book );

So far, so good, – so lets move onto multidimensional arrays.

Sorting multiple arrays and multidimensional arrays with array_multisort()

The PHP array_multisort() function sorts multiple related arrays in one run whilst preserving the relationships between the arrays, and it can also sort multidimensional arrays.

$directors = array( “James Cameron”, “Ethan Cohen”, “Christopher Nolan” );
$titles = array( “Aliens”, “The Big Lebowski”, “Inception” );
$years = array( 1986, 1998, 1973 );

array_multisort( $directors, $titles, $years );

print_r( $directors );
echo “<br />”;
print_r( $titles );
echo “<br />”;
print_r( $years );
echo “<br />”;

Outputs:

Array ( [0] => Christopher Nolan [1] => Ethan Cohen [2] => James Cameron )
Array ( [0] => Inception [1] => The Big Lebowski [2] => Aliens )
Array ( [0] => 2010 [1] => 1998 [2] => 1986 )

Notice how the array_multisort() function has sorted the values in $directors in ascending order and then sorted the other 2 arrays so that the element order matches the order of the sorted $directors array.

Sorting multidimensional arrays

If you pass a multidimensional array to array_multisort() then it sorts by looking at the first element of each nested array.

If 2 elements have the same value then it sorts by the second element, and so on. For example:

$movies = array(
array(
“director” => “Ethan Cohen”,
“title” => “The Big Lebowski”,
“year” => 1998
),
array(
“director” => “James Cameron”,
“title” => “Aliens”,
“year” => 1986
),
array(
“director” => “Christopher Nolan”,
“title” => “Inception”,
“year” => 1973
),
array(
“director” => “James Cameron”,
“title” => “Titanic”,
“year” => 1997
)
);

array_multisort( $movies );
echo “<pre>”;
print_r( $movies );
echo “</pre>”;

Outputs:

Array
(

[0] => Array
(
[director] => Christopher Nolan
[title] => Inception
[year] => 1973
)
[1] => Array
(
[director] => Ethan Cohen
[title] => The Big Lebowski
[year] => 1998
)

[2] => Array
(
[director] => James Cameron
[title] => Aliens
[year] => 1986
)

[3] => Array
(
[director] => James Cameron
[title] => Titanic
[year] => 1997
)
)

So as you can see the array sorting functions in PHP are quite powerful and flexible. This brief post only covers a few options, there are more functions if you require them. Here is a great place to start.