PHP Sorting while Maintaining Key and Value Relations

PHP has many built in array sorting functions. They are simple to use and provide a multitude of sorting options.

I'll cover the sorting in three articles.  These first sort functions all maintain the association between the key fields and array values while sorting. The next sorting article will cover sorting when the key field and value relationship doesn't matter, and the last article will cover custom sorting functions.

To demonstrate sorting, I've created an array, $testArray, that we will use with the various sorting functions. Here's what the array looks like as initialized before sorting.

Initial Array Before Sorting

Asort

To sort an array in PHP most of the commands are easy to use.  The first sort we'll look at is asort().  To use it type,


asort($testArray);

This is the format for most of your sorting commands.  There is no need to assign the array to another variable.  The next time you use the $testArray it will be sorted like this:

asort($testsarray)

Asort() sorts an array so that the array values maintain their associations with their key fields. The sort uses the array values to sort from A-Z, then a-z, then 1 on up.

Arsort

Arsort sorts on the array values the same as asort(), only in reverse.  Here is the function:


arsort($testArray);

and here is the result:

arsort($testArray)


Ksort

Ksort, for key sort, is the same as "asort" except instead of sorting on array values, it sorts on the key fields, and, at the same time, maintains the associations with the array values.

Here's the function:


ksort($testArray);

and here's the result:

ksort($testArray)


Notice that the values for two, and nine are not sorted as in asort, but they are in the key field column.

Krsort

Krsort is the same as ksort only in reverse key field order.

Here's the function:


krsort($testArray);

and here's the result:

krsort($testArray)


Natsort

Natsort, for natural sort, sorts on array values using a natural sort algorithm.  It separates upper and lower case and numbers are ordered like: 1, 2, 10, 12 instead of 1, 10, 12, 2.  Array values and key fields maintain their relationships

Here's the function:


natsort($testArray);

and here's the result:

natsort($testArray)


Natcasesort

Natcasesort, for natural case sort.  This is the same at natsort, only it is case insensitive.  It sorts on array values using a natural sort algorithm.  It separates upper and lower case and numbers are ordered like: 1, 2, 10, 12 instead of 1, 10, 12, 2.  Array values and key fields maintain their relationships

Here's the function:


natcasesort($testArray);

and here's the result:

natcasesort($testArray)



Notice that the upper and lower case are not case sensitive.

If you would like to have your arrays displayed as shown in this article, you can download the program for free at www.newchk.com.  There is documentation on the web site to show you how to install and use the program.

Comments are closed.