Sorting Arrays in PHP

PHP provides multiple inbuilt functions for Sorting Arrays in PHP. In this article, we will list different sorting functions in PHP.

Each and every function is useful in a different way. Also, every function is minutely different from the other. The main points of differences are:

  • Some sorting functions sort an array in PHP on the basis of value, or some by array key while sorting arrays in PHP.
  • Functions can either maintain the original correlation between keys and values or not.
  • Also, array sorting in PHP can in ascending or descending order. Moreover, arrays can be sorted numerically, alphabetically, naturally or in a user-defined order.
  • None of the functions return a new array. They all modify the original array itself.

Sorting Functions

Function NameSorting ByKey AssociationSorting Order
array_multisortvalueAssociative yes, numeric noSort Options
asortValueYesAscending
arsortValueYesDescending
ksortKeyYesAscending
krsortKeyYesDescending
natsortValueYesNatural
natcasesortValueYesNatural
sortValueNoAscending
rsortValueNoDescending
shuffleValueNoRandom
usortValueNoUser Defined
uasortValueYesUser Defined
uksortKeyYesUser Defined
Sorting Arrays in PHP

Examples

Here are some examples of sorting arrays in different conditions in PHP.

Example 1: Sort Integer Array in Ascending Order

For instance, if you want to sort an integer array in ascending order, use the PHP sort Function.

<?php

	$array = array(5, 1, 2, 7, 3);
	sort($array);
	print_r($array);
	/*
	Array
	(
	    [0] => 1
	    [1] => 2
	    [2] => 3
	    [3] => 5
	    [4] => 7
	)
	*/

?>

Similarly, you can sort the array in descending order using PHP rsort Function.

Example 2: Sorting Associative Arrays

However, to sort an associative array in PHP, use the asort Function to maintain the key-value relationship.

<?php

	$array = array('b' => 'banana', 'c' => 'cat', 'a' => 'apple');
	asort($array);
	print_r($array);
	/*
	Array
	(
	    [a] => apple
	    [b] => banana
	    [c] => cat
	)
	*/

?>

Example 3: Passing Comparison Sort Function

Also, you can pass a comparison function in PHP usort Function for sorting arrays in PHP.

<?php
	function compare($a, $b) {
		if ($a == $b) {
			return 0;
		} else if ($a > $b) {
			return 1;
		} else {
			return -1;
		}
	}

	$testArray = array(4, 2, 1, 5, 3);
	usort($testArray, 'compare');
?>

Example 4: Sorting Arrays in PHP by Keys

Similarly, you can sort an Associative Array according to keys instead of values using the ksort Function in PHP.

<?php

	$testArray = array(1 => 5, 0 => 6, 4 => 3, 3 => 2, 2 => 1);
	ksort($testArray);
	print_r($testArray);
	/*
	Array
	(
	    [0] => 6
	    [1] => 5
	    [2] => 1
	    [3] => 2
	    [4] => 3
	)
	*/

?>

Alternatively, you can use the krsort function to sort array in descending order according to keys.

Example 5: Sorting Arrays According to Natural Order Algorithm

Additionally, you can sort arrays using Natural Order Algorithm using the PHP natsort Function.

<?php
        $array = array("13.txt", "10.txt", "3.txt", "1.txt");
        $originalArray = $array;

        //Standard Sorting Algorithm
        sort($array);
        print_r($array);

        //Natural Order Sorting
        natsort($originalArray);
        print_r($originalArray);


?>
OUTPUT:
//Standard Sorting Algorithm
Array
(
    [0] => 1.txt
    [1] => 10.txt
    [2] => 13.txt
    [3] => 3.txt
)

Conclusion

In conclusion, we listed various ways of Sorting Arrays in PHP. Additionally, you can read more about it on Official PHP Documentation.

Spread the Knowledge

Leave a Reply

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