Categories
PHP

Sorting Arrays

The simplest array-sorting functions are sort( ) and rsort( ), which rearrange the elements of the subject array in ascending and descending order, respectively:

  1. Sorting arrays in ascending order
  2. Sorting arrays in descending order
  3. Sorting arrays alphabetically
  4. Using sorting flags

All sorting functions rearrange the elements in the source array itself. Because of this behavior, the sort functions must be passed a variable, not an expression. In this tutorial, we’ll discuss the sort and rsort functions. Both functions sort the subject array based on the values of each element.

<?php
 /*Syntax:
 sort (array &$array, int $flags = SORT_REGULAR): bool
 rsort(array &$array, int $flags = SORT_REGULAR): bool
 */

Sorting Arrays in Ascending Order – sort()

The following example shows the sort( ) function on an array of integers:

<?php
 $numbers = array(24, 19, 3, 16, 56, 8);

 sort($numbers);

 foreach($numbers as $n)
   echo "$n ";
 //3 8 16 19 24 56

 print_r ( $numbers );
 /*
 Array ( [0] => 3
        [1] => 8
        [2] => 16
        [3] => 19
        [4] => 24
        [5] => 56 )
 */

Sorting Arrays in Descending Order – rsort()

The following example shows the rsort( ) function on the same array:

<?php
 $numbers = array(24, 19, 3, 16, 56, 8);
 rsort($numbers);
 foreach($numbers as $n)
   echo "$n ";
 print_r($numbers);

The output of the example shows the elements sorted in reverse order by value:

//56 24 19 16 8 3

Array ( [0] => 56
        [1] => 24
        [2] => 19
        [3] => 16
        [4] => 8
        [5] => 3 )

Sorting Arrays Alphabetically

By default, PHP sorts strings in alphabetical order and numeric values in numeric order. An optional parameter, flag, can be passed to force the string or numeric sorting behavior. In the following example, the PHP constant SORT_STRING sorts the numbers as if they were strings:

<?php
 $numbers = array(24, 19, 3, 16, 56, 8, 171);
 sort($numbers, SORT_STRING);
 print_r($numbers);

The output of the example shows the result:

Array ( [0] => 16
        [1] => 171
        [2] => 19
        [3] => 24
        [4] => 3
        [5] => 56
        [6] => 8 )

Sorting Flags

Numerical arrays can be sorted rather easily by using sort() or rsort(). However, a problem exists if the array contains both numerical and string values (for instance, "2" > "10" but 2 < 10). Therefore, the sorting can be tweaked so that a special data type is used for comparing elements when sorting:

  • SORT_REGULAR – sorts elements according to their data type (the default behavior).
  • SORT_NUMERIC – sorts elements as numbers.
  • SORT_STRING – sorts elements as strings.
  • SORT_LOCALE_STRING – sorts elements as strings, based on the current locale.
  • SORT_NATURAL – sorts elements as strings using “natural ordering” like natsort().
  • SORT_FLAG_CASE – can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

Using SORT_NUMERIC and SORT_STRING flags

<?php
  $a = array('4', 31, '222', 1345);
 
  sort($a, SORT_NUMERIC);
  print_r($a);

  sort($a, SORT_STRING);
  print_r($a);

Here is the output of the preceding listing:

Array
(
    [0] => 4
    [1] => 31
    [2] => 222
    [3] => 1345
)
Array
(
    [0] => 1345
    [1] => 222
    [2] => 31
    [3] => 4
)

If you want to sort the elements of the array in reverse order (descending order), use rsort() (r for reverse). The same optional second parameters are allowed that can be used with sort().

sort( ) and rsort( ) can be used on associative arrays, but the keys are lost. The resulting array contains only the values in the sorted order. Consider the following example:

<?php
 $map = array("o"=>"kk", "e"=>"zz", "z"=>"hh", "a"=>"rr");
 sort($map);
 print_r($map);

The print_r( ) output shows the modified array without the key values:

Array ( [0] => hh [1] => kk [2] => rr [3] => zz )

More Posts on PHP Sorting Arrays: