Searching...
Wednesday 10 July 2013

Sorting Arrays


To be sure, data sorting is a central topic of computer science. Anybody who’s taken an entry-level programming class is well aware of sorting algorithms such as bubble, heap, shell, and quick. This subject rears its head so often during daily programming tasks that the process of sorting data is as common as creating an if conditional or a while loop. PHP facilitates the process by offering a multitude of useful functions capable of sorting arrays (what is an array)  in a variety of manners.

Note: By default, PHP’s sorting functions sort in accordance with the rules as specified by the English language. If you need to sort in another language, say French or German, you’ll need to modify this default behavior by setting your locale using the setlocale() function.

Reversing Array Element Order

The array_reverse() function reverses an array’s element order. Its prototype follows:

array array_reverse(array array [, boolean preserve_keys])

If the optional preserve_keys parameter is set to TRUE, the key mappings are maintained. Otherwise, each newly rearranged value will assume the key of the value previously presiding at that position:

$states = array("Delaware", "Pennsylvania", "New Jersey");
print_r(array_reverse($states));
// Array ( [0] => New Jersey [1] => Pennsylvania [2] => Delaware )

Contrast this behavior with that resulting from enabling preserve_keys:

$states = array("Delaware", "Pennsylvania", "New Jersey");
print_r(array_reverse($states,1));
// Array ( [2] => New Jersey [1] => Pennsylvania [0] => Delaware )

Arrays with associative keys are not affected by preserve_keys; key mappings are always preserved in this case.

Flipping Array Keys and Values

The array_flip() function reverses the roles of the keys and their corresponding values in an array. Its prototype follows:

array array_flip(array array)

An example follows:

$state = array("Delaware", "Pennsylvania", "New Jersey");
$state = array_flip($state);
print_r($state);

This example returns the following:

Array ( [Delaware] => 0 [Pennsylvania] => 1 [New Jersey] => 2 )

Sorting an Array

The sort() function sorts an array, ordering elements from lowest to highest value. Its prototype follows:

void sort(array array [, int sort_flags])

The sort() function doesn’t return the sorted array. Instead, it sorts the array “in place,” returning nothing, regardless of outcome. The optional sort_flags parameter modifies the function’s default behavior in accordance with its assigned value:
  • SORT_NUMERIC: Sorts items numerically. This is useful when sorting integers or floats.
  • SORT_REGULAR: Sorts items by their ASCII value. This means that B will come before a, for instance.
  • SORT_STRING: Sorts items in a fashion that better corresponds with how a human might perceive the correct order. See natsort() for more information about this matter, introduced later in this section.
Consider an example. Suppose you want to sort exam grades from lowest to highest:

$grades = array(42, 98, 100, 100, 43, 12);
sort($grades);
print_r($grades);

The outcome looks like this:

Array ( [0] => 12 [1] => 42 [2] => 43 [3] => 98 [4] => 100 [5] => 100 )

It’s important to note that key/value associations are not maintained. Consider the following example:

$states = array("OH" => "Ohio", "CA" => "California", "MD" => "Maryland");
sort($states);
print_r($states);

Here’s the output:

Array ( [0] => California [1] => Maryland [2] => Ohio )

To maintain these associations, use asort().

Sorting an Array While Maintaining Key/Value Pairs

The asort() function is identical to sort(), sorting an array in ascending order, except that the key/value correspondence is maintained. Its prototype follows:

void asort(array array [, integer sort_flags])

Consider an array that contains the states in the order in which they joined the Union:

$state[0] = "Delaware";
$state[1] = "Pennsylvania";
$state[2] = "New Jersey";

Sorting this array using sort() produces the following ordering (note that the associative correlation are lost, which is probably a bad idea):

Array ( [0] => Delaware [1] => New Jersey [2] => Pennsylvania )

However, sorting with asort() produces the following:

Array ( [0] => Delaware [2] => New Jersey [1] => Pennsylvania )

If you use the optional sort_flags parameter, the exact sorting behavior is determined by its value, as described in the sort() section.

Sorting an Array in Reverse Order

The rsort() function is identical to sort(), except that it sorts array items in reverse (descending) order. Its prototype follows:

void rsort(array array [, int sort_flags])

An example follows:

$states = array("Ohio", "Florida", "Massachusetts", "Montana");
rsort($states);
print_r($states);

It returns the following:

Array ( [0] => Ohio [1] => Montana [2] => Massachusetts [3] => Florida )

If the optional sort_flags parameter is included, the exact sorting behavior is determined by its value, as explained in the sort() section.

Sorting an Array in Reverse Order While Maintaining Key/Value Pairs

Like asort(), arsort() maintains key/value correlation. However, it sorts the array in reverse order. Its prototype follows:

void arsort(array array [, int sort_flags])

An example follows:

$states = array("Delaware", "Pennsylvania", "New Jersey");
arsort($states);
print_r($states);

It returns the following:

Array ( [1] => Pennsylvania [2] => New Jersey [0] => Delaware )
If the optional sort_flags parameter is included, the exact sorting behavior is determined by its value, as described in the sort() section.

0 comments:

Post a Comment

 
Back to top!