PHP arsort() Function

What is PHP arsort() Function?

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

Syntax:

arsort(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");
arsort($languages);
print_r($languages);
?>
</pre>

Output:

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

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);
arsort($languages, SORT_REGULAR); 
print_r($languages);
?>
</pre>

Output:

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

Example 3:

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

Output:

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

Example 4:

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

Output:

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

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("перше" => "один", "друге" => "два", "По-третє" => "три", "Четверте" => "чотири");
arsort($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");

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

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

Output:

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

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");

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

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

Output:

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

Notes on arsort() 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 arsort() Function

The arsort() 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 descending order preserving the keys.

Reference:

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