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.
<?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
This can also be done with the use of sort() function.
<?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
This can be done with the use of rsort() function.
<?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
<?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
This can be done with the use of asort() function.
<?php $myMarks=array("Science"=>49,"Arts"=>39,"Mathematics"=>45); echo "Original array:".implode(" ",$myChoice); echo "</br>"; echo "Sorted array:".implode(" ",arsort($myChoice)); ?>
This can be done with the use of ksort() function.
<?php $myMarks=array("Science"=>49,"Arts"=>39,"Mathematics"=>45); echo "Original array:".implode(" ",$myChoice); echo "</br>"; echo "Sorted array:".implode(" ",ksort($myChoice)); ?>
This can be done with the use of arsort() function.
<?php $myMarks=array("Science"=>49,"Arts"=>39,"Mathematics"=>45); echo "Original array:".implode(" ",$myChoice); echo "</br>"; echo "Sorted array:".implode(" ",arsort($myChoice)); ?>
This can be done with the use of krsort() function.
<?php $myMarks=array("Science"=>49,"Arts"=>39,"Mathematics"=>45); echo "Original array:".implode(" ",$myChoice); echo "</br>"; echo "Sorted array:".implode(" ",krsort($myChoice)); ?>