php tutorial - PHP arsort Function - php programming - learn php - php code - php script




  • In PHP, arsort function is mainly used in associative array.
  • The "arsort()" function is used for sorting an array elements in reverse order according to the values with its key.

php programming Syntax :

arsort($variable name);
click below button to copy the code. php tutorial - team

learn php Sample Code : a simple php program

<!DOCTYPE html>
<html>
    <body>
        <?php
            $var1=array("Lemon"=>"34","Apple"=>"50","Grapes"=>"25");
	    arsort($var1);
	    foreach ($var1 as $x => $x_value) 
	    {
	       echo "key=" .$x .",value=".$x_value;
	       echo("<br>");
	    }
        ?>
    </body>
</html>
click below button to copy the code. php tutorial - team

php examples Code Explanation :

Code Explanation for arsort Function In PHP

  1. In this statement “$var1” variable is defining an associative array using array function with its keys such as “Lemon, Apple and Grape” and the values as “34,50, and 25”.
  2. The var1 value will be sorted using the arsort function where the array keys are sorted in ascending order.
  3. In this statement we use the for each loop that will be used for looping process in an associative array. The first variable of the for loop defines the array name “$var1” then “$x” defines the key, and “$x_value” defines the value.
  4. Echo statement is used for print the value sorted key and their values and the “.” Dot symbol is used for concatenation of the string in php.

php tutorials Sample Output :

output for arsort Function In PHP

  1. php output shows the key values (Lemon, Apple and Grapes) are sorted in descending order (Lemon, Grapes and Apple) according to the values with its keys (1,3,2) as it is.


Related Searches to php arsort function