How to sort array in PHP?

In this post, we show you 2 ways to sort arrays using strnatcmp() and strcmp() functions.

1. Code:

      <?php 
	$array = array("AB3","A01", "E12", "D03", "A02", "D01", "AB10","A1","B10","B2", 2,5,1,9);
	 usort($array, function ($a, $b){
		return strnatcmp($a, $b);
		});
	 print_r($array);
	 echo "<br>";
	 
	usort($array, function ($a, $b){
			return strcmp($a, $b);
		});
	 print_r($array);
    ?> 

2. Example 1:

Input:

array("AB3","A01", "E12", "D03", "A02", "D01", "AB10","A1","B10","B2", 2,5,1,9);

Output:

Array ( [0] => 1 [1] => 2 [2] => 5 [3] => 9 [4] => A01 [5] => A02 [6] => A1 [7] => AB3 [8] => AB10 [9] => B2 [10] => B10 [11] => D01 [12] => D03 [13] => E12 ) 

3. Example 2:

Input:

array("AB3","A01", "E12", "D03", "A02", "D01", "AB10","A1","B10","B2", 2,5,1,9);

Output:

Array ( [0] => 1 [1] => 2 [2] => 5 [3] => 9 [4] => A01 [5] => A02 [6] => A1 [7] => AB10 [8] => AB3 [9] => B10 [10] => B2 [11] => D01 [12] => D03 [13] => E12 )