In this tutorial, you will learn to sort an associative array in ascending order according to value.
In PHP, there is a predefined function asort(), which will help us to sort an Associative array in ascending order according to value.
The Syntax of asort() function is given below:
asort(arrayvariable name);
Here is an example for sorting an associative array in ascending order:
<html> <body> <?php $a=array("fruit1"=>"apple","fruit2"=>"guava","fruit3"=>"orange","fruit4"=>"banana");//here "fruit1"(key1)=>"apple"(value1),"fruit2"(key2)=>"guava"(value2),"fruit3"(key3)=>"orange"(value3),"fruit4"(key4)=>"banana"(value4) asort($a); print_r($a); ?> </body> </html>
Output: Array ( [fruit1] => apple [fruit4] => banana [fruit2] => guava [fruit3] => orange )
Through this program, we are printing the array in ascending order according to value.