To discuss about sorting of PHP array you need to know the following PHP functions. They are,
This function is used to sort array data in ascending order. If data are numeric (integer or real), then they are to be sorted in their numeric order and for string data, data are to be sorted by their ASCII values. On success the original array will be changed to a new array. So if you need the original order for future use you have to store that array in another variable before executing sort function.
sort() example
<?php
//Sorting numeric array.
$array=array(8,9,10,2.2,7);
sort($array);
foreach($array as $ikey=>$d){echo "array[$ikey] = $d<br />";}
echo '---------------------------------------<br />';
//Sorting text array.
$array=array('ramui.com','w3.org','google.com','yahoo.com','YAHOO.COM');
sort($array);
foreach($array as $ikey=>$d){echo "array[$ikey] = $d<br />";}
/*
array[0] = 2.2
array[1] = 7
array[2] = 8
array[3] = 9
array[4] = 10
---------------------------------------
array[0] = YAHOO.COM
array[1] = google.com
array[2] = ramui.com
array[3] = w3.org
array[4] = yahoo.com
*/
?>
asort() performs sorting of an associative array. An associative array is an array in which an unique key index is assigned for every data in it. The function asort() can be best explained by the following example.
<?php
$array=array('ramui'=>'ramui.com','w3'=>'w3.org','google'=>'google.com','yahoo'=>'yahoo.com','yahoo_cap'=>'YAHOO.COM');
asort($array);
echo '<b>asort() example</b><br />';
foreach($array as $ikey=>$d){echo "array[$ikey] = $d<br />";}
echo '---------------------------------------<br />';
sort($array);
echo '<b>sort() example</b><br />';
foreach($array as $ikey=>$d){echo "array[$ikey] = $d<br />";}
/* Output:
asort() example
array[yahoo_cap] = YAHOO.COM
array[google] = google.com
array[ramui] = ramui.com
array[w3] = w3.org
array[yahoo] = yahoo.com
---------------------------------------
sort() example
array[0] = YAHOO.COM
array[1] = google.com
array[2] = ramui.com
array[3] = w3.org
array[4] = yahoo.com
*/
?>
From the above example you can see that change of array order by asort() doesn't change the index key associated with the data.
In sort() function we see that PHP sort numeric data according to their numeric value and for string data it considers the ASCII values. Let's declare an array as,
$array=array('img7.png', 'img8.png', 'img9.png', 'img10.png');
Carefully look on the data, they have some constant text part, 'img' and '.png' and one variable number. As data have some text part if you run sort() or asort() command to perform sort operation they will be treated as string and will be sorted with their ASCII values. But if you manually sort them you will ignore the common text part and sort according to numeric part of the data. Actually the function natsort() does the same job. The function natcasesort() is case insensitive version of natsort().See the example below for further clarifications.
<?php
$array = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');
sort($array);
echo '<b>sort() example</b><br />';
foreach($array as $ikey=>$d){echo "array[$ikey] = $d<br />";}
echo '---------------------------------------<br />';
$array = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');
natsort($array);
echo '<b>natsort() example</b><br />';
foreach($array as $ikey=>$d){echo "array[$ikey] = $d<br />";}
echo '---------------------------------------<br />';
$array = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');
natcasesort($array);
echo '<b>natcasesort() example</b><br />';
foreach($array as $ikey=>$d){echo "array[$ikey] = $d<br />";}
?>
/*Output:
sort() example
array[0] = IMG0.png
array[1] = IMG3.png
array[2] = img1.png
array[3] = img10.png
array[4] = img12.png
array[5] = img2.png
---------------------------------------
natsort() example
array[0] = IMG0.png
array[5] = IMG3.png
array[4] = img1.png
array[3] = img2.png
array[2] = img10.png
array[1] = img12.png
---------------------------------------
natcasesort() example
array[0] = IMG0.png
array[4] = img1.png
array[3] = img2.png
array[5] = IMG3.png
array[2] = img10.png
array[1] = img12.png
*/
The function ksort() is used to sort an associative array in ascending order of the array key. The following example will explain the functioning of ksort().
<?php
$array=array('a'=>'ramui.com','n'=>'w3.org','c'=>'google.com','b'=>'yahoo.com','z'=>'YAHOO.COM');
ksort($array);
foreach($array as $ikey=>$d){echo "array[$ikey] = $d<br />";}
/*Output:
array[a] = ramui.com
array[b] = yahoo.com
array[c] = google.com
array[n] = w3.org
array[z] = YAHOO.COM
*/
?>
These functions work same as sort(), asort(), ksort() respectively, only they sort array in descending order.
class thingy
{
public $prop1;
public $prop2;
static $sortKey;
public function __construct( $prop1, $prop2 )
{
$this->prop1 = $prop1;
$this->prop2 = $prop2;
}
public static function sorter( $a, $b )
{
return strcasecmp( $a->{self::$sortKey}, $b->{self::$sortKey} );
}
public static function sortByProp( &$collection, $prop )
{
self::$sortKey = $prop;
usort( $collection, array( __CLASS__, 'sorter' ) );
}
}
$thingies = array(
new thingy( 'red', 'blue' )
, new thingy( 'apple', 'orange' )
, new thingy( 'black', 'white' )
, new thingy( 'democrat', 'republican' )
);
print_r( $thingies );
thingy::sortByProp( $thingies, 'prop1' );