PHP Array Sort Function
PHP scripts can store data in arrays. An array stores a series of data values in a linear structure. Each element in an array is associated with an index or a key. The first position in an array is at index zero, with the numbers incrementing along the length of the structure. With an associative array, a script can access each element using its ID key rather than its index. PHP array sort functions give programmers a range of options.
-
Sort By Value
-
The PHP language provides a variety of functions to sort arrays. Functions "sort," "asort," "arsort" and "rsort" sort an array by value. This means that the sorting algorithm arranges the array elements in order according to their values. For an array with number types in it, this means sorting the elements in numerical order. For text strings, the function sorts in alphabetical order. The following sample PHP code demonstrates sorting an array by value:
$fruit_array = array("banana", "apple", "orange");
sort($fruit_array);
This code alters the content of the existing array, rather than creating a new array with the elements sorted. The function returns a boolean value of true or false, reflecting the success or failure of the operation.
Sort By Key
-
Associative arrays in PHP use ID keys to refer to elements. Some sort functions arrange the content of an array according to the keys rather than the values. Examples of this include "ksort," "krsort" and "uksort." The following sample PHP code demonstrates sorting an array on the keys:
$animal_array = array("c"=>"cat", "d"=>"dog", "b"=>"monkey", "a"=>"elephant");
ksort($animal_array);
When this code executes, the resulting array will contain the same elements, sorted in alphabetical order by keys. Each value will still be associated with the same key, so when an element moves position, its key also moves.
-
Key Association
-
When sorting arrays in PHP, programmers can choose whether to maintain the association between keys and values. Most functions for sorting arrays do maintain the link between key and value, but exceptions include "sort," "rsort" and "usort." The following sample code demonstrates using the "rsort" function, which sorts the elements in an array, arranging them in reverse order according to value:
$people_array = array("a"=>"amy", "b"=>"mary", "c"=>"james");
rsort($people_array);
This code results in an array with elements sorted in reverse alphabetical order by value, with the original keys completely removed. Following this process, the script can only access the elements by index, not by key.
Ordering
-
PHP array sorting functions can arrange elements, by key or value, in either ascending or descending order. For text strings, this means alphabetical order, so "a" is lower than "c." Functions sorting in reverse order, from high to low, include "arsort," "krsort" and "rsort." Functions sorting in ascending order include "asort," "sort" and "ksort." Alternative sorting functions can order elements using other arrangements, such as the "shuffle" function, which sorts arrays in random order.
-
References
Resources
- Photo Credit Jupiterimages/Photos.com/Getty Images