PHP Sorting Array

Sorting means organising some elements in an order e.g. in ascending order, descending order, increasing order or decreasing order as you require. PHP has some functions which allow you to sort the array in the order you desire.

Sorting operation can be performed on both:

  • Indexed Array
  • Associative Array

To sort alphabetical array in ascending order

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

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

To sort numerical array in inceasing order

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

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

To sort alphabetical array in descending order

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

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

To sort numerical array in decreasing order

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

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

To sort an associative arrays in ascending order based on value

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

Example

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

This will produce following result



To sort an associative arrays in ascending order based on key

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

Example

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

This will produce following result



To sort an associative arrays in descending order based on value

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

Example

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

This will produce following result



To sort an associative arrays in descending order based on key

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

Example

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

This will produce following result





 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.