pTutorial

New way to learn PHP

PHP 5 Sorting of an Array

Soring

One of the powerful and useful feature of arrays in most of languages is that you can sort the elements in any order you like.

For example, if you store 100 students name in an array and you want to see alphabetically, you can sort that array and display.

PHP provides lot of function to sort an array all are listed below

Array Sorting Functions
sort () For sorting indexed arrays in ascending order
rsort() For sorting indexed arrays in descending order
asort() For sorting associative arrays in ascending order
arsort() For sorting associative arrays in descending order
ksort() For sorting associative arrays by key rather than by value in ascending order
krsort() For sorting associative arrays by key rather than by value in descending order
array_multisort() A powerful function that can sort multiple arrays at once, or multidimensional arrays

sort() function

sort() is a very basic function of sorting an array. It sorts the values of the array in ascending order or alphabetical order.

Example of array sorting in ascending alphabetical order

<?php

$cars = array ("alto", "swift", "audi", "ferrari");
echo "Before Sorting An Array :
"; Print_r($cars); sort($cars); echo "
After Sorting An Array :
"; Print_r( $cars); ?>

Example of array sorting in ascending numerical order

<?php

$num=array(40,5,20,12,101);
echo "
Before Sorting An Array :
"; print_r($num); sort($num); $length=count($num); echo "
After Sorting An Array :
"; for($x=0;$x < $length;$x++) { echo $num[$x]; echo "
"; } ?>

rsort() function

rsort() works same as sort function but its revert the order of element (descending order)

Example of array sorting in descending alphabetical order


<?php

$cars = array 
(
"Alto", 
"swift", 
"audi", 
"ferrari"
);
 
echo "Before Sorting An Array :
"; Print_r($cars); rsort($cars); echo "
After Sorting An Array :
"; Print_r($cars); ?>

Example of array sorting in descending numerical order

<?php

$num=array(40,5,20,12,101);
echo "
Before Sorting An Array :
"; print_r($num); rsort($num); $length=count($num); echo "
After Sorting An Array :
"; for($x=0;$x < $length;$x++) { echo $num[$x]; echo "
"; } ?>

asort() function

assort() function works same as sort function except that array indexes maintain their previous position and values in ascending order. asort() function mainly used for sorting an associative arrays.

Example of array sorting in descending alphabatical order

<?php

$name=array(
"Audi"=>"R8",
"Bmw"=>" BMW Z4 M Coupe",
"SKODA"=>"Fabia"
);
asort($name);
echo "Key"."--------". "Value
"; foreach($name as $key=>$key_value) { echo $key ."--------". $key_value; echo "
"; } ?>

arsort() function

arsort() function works same as asort function except that array indexes maintain their previous position and values in descending order. asort() function mainly used for sorting an associative arrays.

Example of array sorting in descending alphabetical order

<?php

$name=array(
"Audi"=>"R8",
"Bmw"=>" BMW Z4 M Coupe",
"SKODA"=>"Fabia"
);
arsort($name);
echo "Key"."--------". "Value
"; foreach($name as $key=>$key_value) { echo $key ."--------". $key_value; echo "
"; } ?>

QUOTES OF THE DAY

Knowledge without integrity is dangerous and dreadful.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER