PHP Sort Array Data

In PHP, sort array data means organising elements of array in ascending or descending order, increasing order or decreasing order as you require. PHP has some functions which hepls you to sort the array value in ascending and descending order as you desire.

Sorting operation can be performed on both:

  • Indexed Array
  • Associative Array

Sort Array Alphabetical in Ascending Order

This can be done with the use of sort() function.

Sort Array Alphabetical in Ascending Order Example

<?php 
$myChice=array("Science","Red","Sunday");
echo "Original array:".implode(" ",$myChoice);
echo "</br>";
echo "Sorted array:".implode(" ",sort($myChoice));
?> 

This will produce following result

Science Red Sunday
Red Science Sunday

Sort Numerical Array in Inceasing Order

This can also be done with the use of sort() function.

Sort Numerical Array in Inceasing Order Example

<?php 
$myMarks=array(49,39,45);
echo "Original array:".implode(" ",$mymarks);
echo "</br>";
echo "Sorted array:".implode(" ",sort($mymarks));
?> 

This will produce following result

Original array:49 39 45
Sorted array:39 45 49

Sort Alphabetical Array in Descending Order

This can be done with the use of rsort() function.

Sort Alphabetical Array in Descending Order Example

<?php 
$myChice=array("Science","Red","Sunday");
echo "Original array:".implode(" ",$myChoice);
echo "</br>";
echo "Sorted array:".implode(" ",rsort($myChoice));
?> 

This will produce following result

Science Red Sunday
Sunday Science Red

Sort Numerical Array in Decreasing Order

This can also be done with the use of rsort() function.

Sort Numerical Array in Decreasing Order Example

<?php 
$myMarks=array(49,39,45);
echo "Original array:".implode(" ",$mymarks);
echo "</br>";
echo "Sorted array:".implode(" ",rsort($mymarks));
?> 

This will produce following result

Original array:49 39 45
Sorted array:49 45 34

Sort an Associative Arrays in Ascending Order

This can be done with the use of asort() function.

Sort an Associative Arrays in Ascending Order Example

<?php 
$myMarks=array("Science"=>49,"Arts"=>39,"Mathematics"=>45);
echo "Original array:".implode(" ",$myChoice);
echo "</br>";
echo "Sorted array:".implode(" ",arsort($myChoice));
?> 

Sort an Associative Arrays in Ascending Order

This can be done with the use of ksort() function.

Sort an Associative Arrays in Ascending Order Example

<?php 
$myMarks=array("Science"=>49,"Arts"=>39,"Mathematics"=>45);
echo "Original array:".implode(" ",$myChoice);
echo "</br>";
echo "Sorted array:".implode(" ",ksort($myChoice));
?> 

Sort an Associative Arrays in Descending Order

This can be done with the use of arsort() function.

Sort an Associative Arrays in Descending Order Example

<?php 
$myMarks=array("Science"=>49,"Arts"=>39,"Mathematics"=>45);
echo "Original array:".implode(" ",$myChoice);
echo "</br>";
echo "Sorted array:".implode(" ",arsort($myChoice));
?> 

Sort an Associative Arrays in Descending Order

This can be done with the use of krsort() function.

Sort an Associative Arrays in Descending Order Example

<?php 
$myMarks=array("Science"=>49,"Arts"=>39,"Mathematics"=>45);
echo "Original array:".implode(" ",$myChoice);
echo "</br>";
echo "Sorted array:".implode(" ",krsort($myChoice));
?>