Tutorialsplane

PHP Sorting Array


PHP Array Sorting:– Array sorting means reordering of array element either in ascending or descending order.


PHP Indexed Array Sorting | Example

To sort indexed array we use sort() function whereas rsort() function is used to sort in descending order.

Example of Indexed Array Sorting in ascending order.

<?php $numbers = array(4, 6, 2, 22, 11);
sort($numbers);
print_r($numbers);
??>

PHP Associative Array Sorting| Example

we use asort() function To sort associative array in ascending order according to value whereas ksort() function is used to sort according to key value.

arsort() and krsort() functions are used to sort associative array in desdending order according to value and key respectively.

Example of Associative Array Sorting in ascending order by value and key respectively in below example.

Example of Associative Array Sorting

<?php $numbers = array("Sohan"=?>10,"Mohan"=>8,"Rohan"=>11,"Kamal"=>22,"Paras"=>21);
ksort($numbers);
print_r($numbers);
?>

PHP Tutorial