Networking, Programming and Graphics - Tutorials
ONLINEHOWTO.net Tutorials Category

PHP Array Sort

Type: Code Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Level: Beginner Networking, Programming and Graphics - Tutorials 
Networking, Programming and Graphics - Tutorials
Date: 2009-Apr-01
Networking, Programming and Graphics - Tutorials
Visited: 5887 times
Networking, Programming and Graphics - Tutorials
Rating: Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Published: Ivory Morhuld

Like in any other programming language PHP can work with arrays. PHP Arrays can be two, tree or more dimensional. PHP Arrays can contain strings, integers, arrays, etc.

When we fill an array with data this data can be some random data. Later, when we want to display this data, we want to get sorted and structured result. We can this with PHP Array Sort functions.

In this tutorial I will show you the main sort unctuion: sort(), asort(), ksort(), natsort() and their derivatives.
sort()

This is a simplest array sort function in PHP. This function make a simple sort of the array values. When you use rsort() this will sort the array in revers order. Function returns TRUE on success or FALSE on failure.

Please note that sort() function will remove existing key assignment and will reorder array by assigning new indexes and they always will be in 0, 1, 2, 3..... order.

Also be careful when sorting arrays with mixed or unknown types values because sort() can produce unpredictable results.

asort()

This function works just like sort() function, but asort() will keep index association. When you use arsort() this will sort the array in revers order. Function returns TRUE on success or FALSE on failure.
<?php
$cars
=array('Ford''Chevrolet''Mercedes''Chrysler''Dodge');
sort($cars);
foreach (
$cars as $key => $value) {
    echo 
"cars[".$key."]=".$value."\n";
}
?>

The above example will display:

cars[0]=Chevrolet
cars[1]=Chrysler
cars[2]=Dodge
cars[3]=Ford
cars[4]=Mercedes

======================================================================

<?php
$cars
=array('Ford''Chevrolet''Mercedes''Chrysler''Dodge');
asort($cars);
foreach (
$cars as $key => $value) {
    echo 
"cars[".$key."]=".$value."\n";
}
?>

The above example will display:

cars[1]=Chevrolet
cars[3]=Chrysler
cars[4]=Dodge
cars[0]=Ford
cars[2]=Mercedes
ksort()

Ksort sorts array by keys. When you use krsort() this will sort the array in revers order. Function returns TRUE on success or FALSE on failure.
<?php
$cars
=array('x'=>'Ford''r'=>'Chevrolet''a'=>'Mercedes''w'=>'Chrysler''f'=>'Dodge');
ksort($cars);
foreach (
$cars as $key => $value) {
    echo 
"cars[".$key."]=".$value."\n";
}
?>

The above example will display:

cars[a]=Mercedes
cars[f]=Dodge
cars[r]=Chevrolet
cars[w]=Chrysler
cars[x]=Ford

======================================================================

<?php
$cars
=array('x'=>'Ford''r'=>'Chevrolet''a'=>'Mercedes''w'=>'Chrysler''f'=>'Dodge');
krsort($cars);
foreach (
$cars as $key => $value) {
    echo 
"cars[".$key."]=".$value."\n";
}
?>

The above example will display:

cars[x]=Ford
cars[w]=Chrysler
cars[r]=Chevrolet
cars[f]=Dodge
cars[a]=Mercedes
natsort()

This function sorts an array by using a "natural order" algorithm. The main difference can be seen by sorting array with integer values. Function returns TRUE on success or FALSE on failure.
<?php
$array1
=array("pic12.gif""pic2.gif""pic10.gif""pic1.gif");
$array2=$array1;

sort($array1);
echo 
"Standard sorting:\n";
foreach (
$array1 as $key => $value) {
    echo 
"array1[".$key."]=".$value."\n";
}

natsort($array2);
echo 
"Natural order sorting:\n";
foreach (
$array2 as $key => $value) {
    echo 
"array2[".$key."]=".$value."\n";
}
?>

The above example will output:

Standard sorting:
array1[0] => pic1.gif
array1[1] => pic10.gif
array1[2] => pic12.gif
array1[3] => pic2.gif

Natural order sorting:
array2[3] => pic1.gif
array2[1] => pic2.gif
array2[2] => pic10.gif
array2[0] => pic12.gif
Rate this tutorial:                    
Post Comment

    • ( onitan [ at ] onitan . org ),

      posted on 2009-Jun-16 | 01:43:19 AM
      Interesting, nice explanation about sorting with PHP.

      Thank you :)
Need a specific tutorial? Do not hesitate and submit a request!
Your e-mail: