PHP Program to Sort Associative Array in Descending Order According to Value?

PHP Program to Sort Associative Array in Descending Order According to Value?

  • PHP
  • 1 min read

Here is an example of a PHP program to sort an associative array in descending order according to the value.

In PHP, a predefined function arsort() is used to sort an Associative array in descending order according to value.

In PHP the Syntax of arsort() function is:

arsort(arrayvariable name);

Here is an example for sorting an associative array in descending order:

<html>
<body>
<?php
$a=array("name1"=>"sunday","name2"=>"monday","name3"=>"friday","name4"=>"saturday");//here "name1"(key1)=>"sunday"(value1),"name2"(key2)=>"monday"(value2),"name3"(key3)=>"friday"(value3),"name4"(key4)=>"saturday"(value4)
arsort($a);//$a is the array variable name
print_r($a);
?>
</body>
</html>

Output: Array ( [name1] => sunday [name4] => saturday [name2] => monday [name3] => friday ).
print_r($a) is using in this program to print the array.

See also: