PHP asort() Function

What is PHP asort() Function?

If you want to sort an associative array in ascending order preserving its keys, use asort() function.

Syntax:

asort(array, flag)

Parameters:

The Function has 1 required parameter and 1 optional parameter-

array (Required): it specifies an array. Check the example 1.

flag (Optional): It specifies a constant that describes how to compare array items. It has 6 possible values-

  • SORT_REGULAR – it is the default value. It doesn’t change data type of elements if there exists mixed data types (ex. string, numbers) and it uses standard PHP comparison rules. Note, it may lead unexpected results. Check example 2.
  • SORT_NUMERIC – It compares array elements as numeric. Use this when you want to sort an array that has both numeric string and integer. Check example 3.
  • SORT_STRING – It compares array elements as strings. Check example 4.
  • SORT_LOCALE_STRING – It compares array elements as strings based on currently set locale. You must set the locale using setlocale() function before to get correct result from SERT_LOCALE_STRING flag. Check example 5.
  • SORT_NATURAL – It compares array elements as string using natural ordering like a human. So, “version8” comes before “version10”. Check example 6.
  • SORT_FLAG_CASE – It compares case-insensitively array elements as string using natural ordering like a human. So, “Version10” comes before “version1”. It can be used using | (bitwise operator) with SORT_NATURAL or SORT_STRING. Check example 7.

Return Values:

This function always returns TRUE. Note: the function doesn’t return the sorted array, instead, it modifies the original array by changing the element order.

Examples:

Example 1:

<pre>
<?php
$languages = array("Markup" => "HTML", "Styling" => "CSS", "Scripting" => "Javascript", "Serverside" => "PHP");
asort($languages); 
print_r($languages);
?>
</pre>

Output:

Array
(
[Styling] => CSS
[Markup] => HTML
[Scripting] => Javascript
[Serverside] => PHP
)

Example 2:

<pre>
<?php
$array = array(300, 200, 400, 100, "PHP", "MySQL");
$languages = array("Markup" => "HTML", "Styling" => "CSS", "Scripting" => "Javascript", "Serverside" => "PHP", "PHPVersion" => 8.5, "HTMLVersion" => 5);
asort($languages, SORT_REGULAR); 
print_r($languages);
?>
</pre>

Output:

Array
(
[HTMLVersion] => 5
[PHPVersion] => 8.5
[Styling] => CSS
[Markup] => HTML
[Scripting] => Javascript
[Serverside] => PHP
)

Example 3:

<pre>
<?php
$array = array("PHPVersion" => 8.5, "HTMLVersion" => 5, "CSSVersion" => 5, "JSVersion" => 16, "MySQLVersion" => "9.5.0");
asort($array, SORT_NUMERIC);
print_r($array);
?>
</pre>

Output:

Array
(
[HTMLVersion] => 5
[CSSVersion] => 5
[PHPVersion] => 8.5
[MySQLVersion] => 9.5.0
[JSVersion] => 16
)

Example 4:

<pre>
<?php
$array = array("First" => "500", "Second" => "200", "Third" => "400", "Fourth" => "1001", "Fifth" => "100");
asort($array, SORT_STRING);
print_r($array);
?>
</pre>

Output:

Array
(
[Fifth] => 100
[Fourth] => 1001
[Second] => 200
[Third] => 400
[First] => 500
)

Example 5:

<pre>
<?php /* The exampla below sorts the Ukrainian strings in ascending order.*/
setlocale(LC_ALL, 'uk_UA.UTF-8', 'uk_UA.utf8', 'uk_UA', 'ukr_UKR', 'ukr');
$array = array("перше" => "один", "друге" => "два", "По-третє" => "три", "Четверте" => "чотири");
asort($array, SORT_LOCALE_STRING);
print_r($array);
?>
</pre>

Output:

Array
(
[друге] => два
[перше] => один
[По-третє] => три
[Четверте] => чотири
)

Example 6:

<pre>
<?php
$array_standard = array("PHPVersion" => "Version8.5", "HTMLVersion" => "Version5", "CSSVersion" => "Version5", "JSVersion" => "Version16", "MySQLVersion" => "Version9.5.0");
$array_natural  = array("PHPVersion" => "Version8.5", "HTMLVersion" => "Version5", "CSSVersion" => "Version5", "JSVersion" => "Version16", "MySQLVersion" => "Version9.5.0");

asort($array_standard);
echo "After Standard Sorting:\n";
print_r($array_standard);

asort($array_natural, SORT_NATURAL);
echo "After Natural Sorting:\n";
print_r($array_natural);
?>
</pre>

Output:

After Standard Sorting:
Array
(
[JSVersion] => Version16
[HTMLVersion] => Version5
[CSSVersion] => Version5
[PHPVersion] => Version8.5
[MySQLVersion] => Version9.5.0
)
After Natural Sorting:
Array
(
[HTMLVersion] => Version5
[CSSVersion] => Version5
[PHPVersion] => Version8.5
[MySQLVersion] => Version9.5.0
[JSVersion] => Version16
)

Example 7:

<pre>
<?php
$version_standard = array("First" => "Version10", "Second" => "version7", "Third" => "Version8.5", "Fourth" => "version1");
$version_natural  = array("First" => "Version10", "Second" => "version7", "Third" => "Version8.5", "Fourth" => "version1");

asort($version_standard);
echo "After Standard Sorting:\n";
print_r($version_standard);

asort($version_natural, SORT_NATURAL | SORT_FLAG_CASE);
echo "After Natural case-insensitive Sorting:\n";
print_r($version_natural);
?>
</pre>

Output:

After Standard Sorting:
Array
(
[First] => Version10
[Third] => Version8.5
[Fourth] => version1
[Second] => version7
)
After Natural case-insensitive Sorting:
Array
(
[Fourth] => version1
[Second] => version7
[Third] => Version8.5
[First] => Version10
)

Notes on asort() Function:

  • If multiple elements have same values, the function doesn’t change their order.
  • After sorting, the function set the internal pointer to the first element in the array.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP asort() Function

The asort() function is a built-in PHP function and part of the PHP’s array functions. It is a quick and effective way to sort array elements in ascending order preserving the keys.

Reference:

https://www.php.net/manual/en/function.asort.php