Sorting Arrays
I was working on a client project last night. I had to sort a multidimensional array. I came across a neat PHP function called asort. In the end, I had to use arsort instead of asort to get a reverse sort order. This short article will show you how to sort arrays in normal and reverse order. Here’s our array.
<pre lang="html">
$animals = array("a" => "Giraffe", "b" => "Elephant", "c" => "Lion");
We will sort the array using the asort function. We print the array to view the results.
<pre lang="html">
asort($animals);
print_r($animals);
The asort output is:
<pre lang="html">
Array ( [b] => Elephant [a] => Giraffe [c] => Lion )
The arsort (reverse order) output is:
<pre lang="html">
Array ( [c] => Lion [a] => Giraffe [b] => Elephant )