Sorting Array in PHP

PHP provides a lot of built-in functions those deal with sorting PHP arrays.  PHP array elements can be sorted in alphabetically or numerically, ascending or descending order. This article will show how to sort an array elements using PHP built-in array sorting functions.

Sorting Array Elements

Sorting Array Elements

Sorting PHP Numeric Array

PHP Numeric array can be sorted using the sort() function. See below example where the sort() function is being used to rearrange a numeric array alphabetically.


<?php
 $fruits=array('Orange','Mango', 'Apple', 'Banana');
 sort($fruits);
   foreach($fruits as $fruit){
     echo $fruit.' ';
   }
?>

Output: Apple Banana Mango Orange

The sort() function can optionally take second parameter for deciding sorting behavior. There are four built-in options those can be used as the second parameter…

  1. SORT_REGULAR
  2. SORT_NUMERIC
  3. SORT_STRING
  4. SORT_LOCAL_STRING

All options are self-described. So, this article is omitting further description.

PHP array elements can also be sorted reversely with the rsort() function. See below example where an array elements are being sorted reversely with the rsort() function.


<?php
  $numbers=array(5,3,7,25,17,50,45);
  rsort($numbers);
  foreach($numbers as $number){
   echo $number.' ';
  }
?>

Output: 50 45 25 17 7 5 3

Sorting PHP Associative Array

There are two ways to sort a PHP associative array. These are..

  1. Array element sorting
  2. Array key sorting

PHP associative array elements can be sorted with the asort() function. An example of sorting associative array elements using asort() function is given below…


<?php
  $fruits=array('1'=>'Orange','9'=>'Mango', '2'=>'Apple', '5'=>'Banana');
  asort($fruits);
  foreach($fruits as $key => $value){
    echo $key.' = '.$value."</br>";
  }
?>

Output

2 = Apple
5 = Banana
9 = Mango
1 = Orange

Similarly associative array can also be sorted according to key values with the ksort() function. See below example where the above example is being sorted according to key values with the ksort() function.


<?php
 $fruits=array('1'=>'Orange','9'=>'Mango', '2'=>'Apple', '5'=>'Banana');
 ksort($fruits);
 foreach($fruits as $key => $value){
     echo $key.' = '.$value."</br>";
}

?>

Output

1 = Orange
2 = Apple
5 = Banana
9 = Mango

Sorting Multidimensional Array

PHP Multidimensional array can be sorted with the array_multisort() function. See below example where a multidimensional array is being sorted with the array_multisort() function.


<?php
  $ar=array(array('Banana','Apple','Orange','Mango'),array(5,10,7,9));
  array_multisort($ar[0],SORT_ASC,SORT_STRING,$ar[1],SORT_DESC,SORT_NUMERIC);
  foreach($ar as $a){
   foreach($a as $value){
     echo $value.' ';
   }
     echo "</br>";
   }
?>

Output

Apple Banana Mango Orange
10 5 9 7

Here, the first array is being sorted but the second array is just being re-indexed according to first array sorting. You can find more details about array_multisort() function on PHP manual page.

With the array_multisort() function not only multidimensional array but also multiple arrays can also be sorted. See another example where two arrays are being sorted with the array_multisort() function.


<?php
  $ar1 = array(10, 5, 25, 20);
  $ar2 = array(1, 3, 2, 4);
  array_multisort($ar1, $ar2);
   foreach($ar1 as $value){
     echo $value.' '; }
    echo "</br>";
   foreach($ar2 as $value){
     echo $value.' ';
   }
 ?>

Output

5 10 20 25
3 1 4 2

Sorting PHP array elements with built-in array functions has been explained in this article. If you face any problem to understand this article, feel free to contact with me from Contact us page. I’ll try my best to stay with you.

Leave a Comment

Your email address will not be published. Required fields are marked *