A Server Side Web Tutorial #6 - PHP Sorting Arrays

Recap of the Last Tutorial & Goals for This One

In our last tutorial, A Server Side Web Tutorial #5 - Working with PHP Indexed, Associative, and Multidimensional Arrays we defined the three types of arrays PHP supports. We then demonstrated the use of looping construct to list the array contents.

In this tutorial we will be mainly concerned with sorting arrays. An array can be sorted in alphabetical or numerical order, descending or ascending.

The PHP sort() Function

  • There are a number of flags which can be used with sort()::
  • SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively
  • SORT_LOCALE_STRING - compare items as strings, based on the current locale.It uses the locale, which can be changed using setlocale()
  • SORT_NATURAL - compare items as strings using "natural ordering" like natsort(), see the example in the snapshots which follow. Basically, in will sort in an order that would be used by a human.
  • SORT_NUMERIC - compare items numerically
  • SORT_REGULAR - compare items normally
  • SORT_STRING - compare items as strings

An important thing to recognize about sort(). It is a boolean function which will return true of false based on the sort being successful or not.

Performing a Simple Sort With sort()

A very simple sort. No addition flags are set. The order in which the items are arranged is a normal acending alphabetical sort.
A very simple sort. No addition flags are set. The order in which the items are arranged is a normal acending alphabetical sort.

A Natural Order Sort Using natsort()

Notice the difference between an alphabetic sor ascending and a natural sort.  Huans would think this way 1 is before two and 10 is after 2. Computers "think" differently 1 is followed by 10 and then 2, etc.
Notice the difference between an alphabetic sor ascending and a natural sort. Huans would think this way 1 is before two and 10 is after 2. Computers "think" differently 1 is followed by 10 and then 2, etc.

A Numeric Sort

A simple numeric sort (ascending), negative numbers precede positive values.
A simple numeric sort (ascending), negative numbers precede positive values.

The PHP rsort() "Reverse" Sort Function

The rsort() function sorts strings or numeric values in reverse order. "rsort()" like sort() is a boolean function returning true or false based on whether the sort was successful or not. rsort(0 used the same flas as sort().

rsort() vs. natsort() For String Values

The rsort() without  the SORT_NATURAL fag be set does not match the intuitive way that humans would want the data sorted.
The rsort() without the SORT_NATURAL fag be set does not match the intuitive way that humans would want the data sorted.

Sorting An Associative Array Using asort() and arsort()

asort() is used to sort an associative array in ascending values.while arsort() is used to sort the array in descending values. The functions are boolean returning true or false based on the success or failure of the sort. The sort preserves the association between key and value pair.

Examples of Associative Function asort() and arsort() Use

An example of the sort of an associative array with asort(0. Note, the key/value relationship is preserved. The sort is in terms of ascending values.
An example of the sort of an associative array with asort(0. Note, the key/value relationship is preserved. The sort is in terms of ascending values.
A simple example or an arsort(). Just think "r" for reverse order.
A simple example or an arsort(). Just think "r" for reverse order.

Sorting According to Key Values: ksort() and krsort()

Instead of sorting by the value in a key/value array. One can sort based on the key value with the PHP ksort() function.in ascending order or krsort() to sort in descending order. The same set of flags used by sort(0 can be used by these functions. Again, as with all the other functions we have seen up to now, ksort() and krsort(0 return a boolean value of true or false.

Examples of the Use of ksort() and krsort()

ksort() is used to sort an associative array in ascending key value.
ksort() is used to sort an associative array in ascending key value.
krsort(0 is used to sort an associative array in descending value.
krsort(0 is used to sort an associative array in descending value.

The array_unique() Function

At times it may be desirable to remove duplicate values from an array. The PHP function array_unigue() does this.

Example of the Use of array_unique()

array_unique(0 returns a new array with duplicate values removed.
array_unique(0 returns a new array with duplicate values removed.

Finding Differences Between Arrays

The array_diff() function compares two arrays. The result is the differences in values ONLY.

The array_diff_key() function compares not only the value but also the key. If the values are the same and the keys are different, the function will in this in reporting the differences.

In this example, the first array has four elements while the third array has three. The fourth key/value pair is reported as a diff.
In this example, the first array has four elements while the third array has three. The fourth key/value pair is reported as a diff.
In this example, the arrays are similar to the previous screen shot. However in addition to the diff on the fourth term two keys b and c have different values. They are report as being diffs.
In this example, the arrays are similar to the previous screen shot. However in addition to the diff on the fourth term two keys b and c have different values. They are report as being diffs.

The array_sum() Function

The array_sum(0 function counts the occurrences of the values in the array.

An Example of the Use of array_count_value()

The array_count_values() can be a very useful function to have when knowing the frequency of some object occurring is needed
The array_count_values() can be a very useful function to have when knowing the frequency of some object occurring is needed

Wrap Up and What's Next

In this tutorial we started looking at different ways of sort an array. The functions occur in pairs to indicated ordering ascending or descending keys or values. The functions are:

  • sort() - sort arrays in ascending order
  • rsort() - sort arrays in descending order
  • asort() - sort associative arrays in ascending order by value
  • ksort() - sort associative arrays in ascending order by key
  • arsort() - sort associative arrays in descending order,by value
  • krsort() - sort associative arrays in descending by key

We then covered a "sampling" of several useful built-in functions for working with arrays, We will see many more!

As far as the next tutorial we will turn our attention to how PHP interacts with HTML forms.


Please Rate This Tutorial as to its Usefulness

Cast your vote for Did this tutorial present information useful to you?

More by this Author


No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working