MENU
Sorting
The sorting functions are:sort(&$arr[,$i=SORT_REGULAR])
rsort(&$arr[,$i=SORT_REGULAR])
usort(&$arr,$cf($m1,$m2))
ksort(&$arr[,$i=SORT_REGULAR])
krsort(&$arr[,$i=SORT_REGULAR])
uksort(&$arr,$cf($m1,$m2))
asort(&$arr[,$i=SORT_REGULAR])
arsort(&$arr[,$i=SORT_REGULAR])
uasort(&$arr,$cf($m1,$m2))
natsort(&$arr)
natcasesort(&$arr)
‘k’ sorts the keys instead of the values. ‘a’ maintains the associations with keys. ‘r’ sorts the elements in reverse order. ‘nat’ sorts the elements in natural order. ‘natcase’ performs a case-insensitive sort on the elements, in natural order. ‘u’ requires a comparison function to be supplied. The comparison function $cf has to return an integer < 0 if the first argument is less than the second, 0 if the first argument equals the second, > 1 if the first argument is greater than the second. $i can be SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_FLAG_CASE, SORT_LOCALE_STRING, SORT_NATURAL. These functions return TRUE on success, FALSE on failure.
<!DOCTYPE html><html><head></head>
<body>
<?php
function s($m1,$m2){
return strcmp($m1,$m2);
}
$a = ["c"=>"lemon","a"=>"orange","z"=>"banana","e"=>"apple","g"=>"watermelon"];
$b = $a; sort($b) ; print_r($b); echo "<br />";
$b = $a; usort($b,'s'); print_r($b); echo "<br />";
$b = $a; ksort($b) ; print_r($b); echo "<br />";
$b = $a; asort($b) ; print_r($b); echo "<br /><br />";
$a = ["img.gif","img1.gif","img11.gif","img2.gif"];
$b = $a; sort($b); print_r($b); echo "<br />";
$b = $a; natsort($b); print_r($b); echo "<br /><br />";
$a = ["c"=>"Lemon","a"=>"orange","z"=>"Banana","e"=>"apple", "g"=>"watermelon"];
$b = $a; sort($b); print_r($b); echo "<br />";
$b = $a; sort($b,SORT_STRING|SORT_FLAG_CASE); print_r($b); echo "<br />";
?></body></html>
Array ( [0] => apple [1] => banana [2] => lemon [3] => orange [4] => watermelon ) Array ( [0] => apple [1] => banana [2] => lemon [3] => orange [4] => watermelon ) Array ( [a] => orange [c] => lemon [e] => apple [g] => watermelon [z] => banana ) Array ( [e] => apple [z] => banana [c] => lemon [a] => orange [g] => watermelon ) Array ( [0] => img.gif [1] => img1.gif [2] => img11.gif [3] => img2.gif ) Array ( [0] => img.gif [1] => img1.gif [3] => img2.gif [2] => img11.gif ) Array ( [0] => Banana [1] => Lemon [2] => apple [3] => orange [4] => watermelon ) Array ( [0] => apple [1] => Banana [2] => Lemon [3] => orange [4] => watermelon )
For array_multisort(), $b is sorted according to the order by which $a is sorted. When $a contains the same values, $b is sorted according to its own sorting order.
<!DOCTYPE html><html><head></head>
<body>
<?php
$a = ["c"=>"lemon","orange","banana","apple","watermelon"];
print_r(array_reverse($a)); echo "<br />";
shuffle($a); print_r($a); echo "<br /><br />";
$a = ["10",11,100,100,100,"a"];
$b = [ 1, 2, "2", 3, 6,"1"];
array_multisort($a,$b);
print_r($a); echo "<br />";
print_r($b); echo "<br /><br />";
array_multisort($a,SORT_ASC,SORT_STRING,$b,SORT_NUMERIC,SORT_DESC);
print_r($a); echo "<br />";
print_r($b); echo "<br /><br />";
?></body></html>
Array ( [0] => watermelon [1] => apple [2] => banana [3] => orange [c] => lemon ) Array ( [0] => apple [1] => orange [2] => lemon [3] => banana [4] => watermelon ) Array ( [0] => 10 [1] => a [2] => 11 [3] => 100 [4] => 100 [5] => 100 ) Array ( [0] => 1 [1] => 1 [2] => 2 [3] => 2 [4] => 3 [5] => 6 ) Array ( [0] => 10 [1] => 100 [2] => 100 [3] => 100 [4] => 11 [5] => a ) Array ( [0] => 1 [1] => 6 [2] => 3 [3] => 2 [4] => 2 [5] => 1 )