PHP asort Function | Sort Associative Arrays Ascending Order

PHP asort is an inbuilt Function in PHP. It sorts arrays in ascending order while maintaining the relationship between keys and values. The smallest element in the input array appears first and the largest appears last. Also, it provides six sorting types to change the sorting behavior. In this article, we will discuss the PHP asort Function. Also, we will discuss a few examples of using it.

In order to sort an array in descending order, you can use PHP arsort Function.

Note: This function is mostly helpful in sorting Associative Arrays because the because it preserves the relation between keys and values.

Syntax

bool asort( $array, $sortingType = SORT_REGULAR )

Parameters

The PHP asort Function expects two parameters. However, only the first parameter is mandatory.

  • $array: The first parameter to the function is an array which you need to sort. Also, it is a mandatory parameter.
  • $sortingType: The function provides 6 sorting types which are optional to pass. This parameter can take the following values:
    • SORT_REGULAR: On passing SORT_REGULAR or 0, the function compares the elements normally without changing their type. Also, this is the default value to the parameter.
    • SORT_NUMERIC: On passing SORT_NUMERIC or 1, the function compares the elements numerically.
    • SORT_STRING: The function compares the elements string-wise on passing SORT_STRING or 2.
    • SORT_LOCALE_STRING: The function compares the elements as a string based on current locale on passing SORT_LOCALE_STRING or 3. It uses the locale which can be changed by the setlocale function.
    • SORT_NATURAL: On passing 4 or SORT_NATURAL, the function compares the elements as strings using natural ordering.
    • SORT_FLAG_CASE: On passing 5 or SORT_FLAG_CASE, the function compares the elements as case-insensitive strings. Also, this parameter can be used | (bitwise) with SORT_NATURAL or SORT_STRING.

Return Value

The PHP asort Function returns true on success and false on failure. However, it modifies the original input array.

Note: The function preserves the keys of the original array after sorting. Also, if two values are equal, their relative order in the sorted array is undefined.

Examples

Let’s discuss a few examples of using the asort Function in PHP.

Example 1: Sort Integer Array in Ascending Order

First, let’s sort an array using the PHP sort Function.

<?php

	$array = array(5, 1, 2, 7, 3);
	sort($array);
	print_r($array);
        //Keys of the original array not preserved.
	/*
	Array
	(
	    [0] => 1
	    [1] => 2
	    [2] => 3
	    [3] => 5
	    [4] => 7
	)
	*/

?>

You can observe, the function re-indexes the output array. It does not preserve the keys of the original array.

Now, let’s sort the same array using PHP asort Function.

<?php
	$array = array(5, 1, 2, 7, 3);
	asort($array);
	print_r($array);
        //Keys are preserved
	/*
	Array
	(
	    [1] => 1
	    [2] => 2
	    [4] => 3
	    [0] => 5
	    [3] => 7
	)
	*/

?>

You can observe that the function preserves the original keys of the input array.

Example 2: Sort Strings in Ascending Order

Since, the PHP asort function preserves the keys of the original array, it is more useful in sorting associative arrays.

<?php

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

?>

Conclusion

In conclusion, we discussed the PHP asort Function in the above article. You can read more about it on PHP Official Documentation. Additionally, you can learn about more PHP Array Functions on Concatly.

Spread the Knowledge

Leave a Reply

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