Home > Article > Backend Development > How to sort associative array in PHP? (code example)
This article mainly introduces how to use PHP to sort associative arrays.
For PHP learners, arrays are a very important knowledge point. The so-called array is the ability to store one or more values in separate variable names. An indexed array is an array with a numeric index, an associative array is an array with a specified key, and a multidimensional array is an array containing one or more arrays.
Below we will introduce to you various sorting methods of associative arrays through simple examples.
The code example is as follows:
<?php echo "Associative array : Ascending order sort by value"; $array2=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40"); asort($array2); foreach($array2 as $y=>$y_value) { echo "Age of ".$y." is : ".$y_value." "; } echo "Associative array : Ascending order sort by Key"; $array3=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40"); ksort($array3); foreach($array3 as $y=>$y_value) { echo "Age of ".$y." is : ".$y_value.""; } echo "Associative array : Descending order sorting by Value"; $age=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40"); arsort($age); foreach($age as $y=>$y_value) { echo "Age of ".$y." is : ".$y_value.""; } echo "Associative array : Descending order sorting by Key"; $array4=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40"); krsort($array4); foreach($array4 as $y=>$y_value) { echo "Age of ".$y." is : ".$y_value." "; } ?>
The output result is as follows:
1. In ascending order by value
Associative array : Ascending order sort by value Age of Sophia is : 31 Age of William is : 39 Age of Ramesh is : 40 Age of Jacob is : 41
2. Sort the associative array in ascending order by key name:
Associative array : Ascending order sort by Key Age of Jacob is : 41 Age of Ramesh is : 40 Age of Sophia is : 31 Age of William is : 39
3. Sort in descending order by value
Associative array : Descending order sorting by Value Age of Jacob is : 41 Age of Ramesh is : 40 Age of William is : 39 Age of Sophia is : 31
4. Sort the associative array in descending order according to the key name:
Associative array : Descending order sorting by Key Age of William is : 39 Age of Sophia is : 31 Age of Ramesh is : 40 Age of Jacob is : 41
Introduction to related functions:
arsort() Function pair association The array is sorted in descending order by key value.
asort() The function sorts the associative array in ascending order by key value.
krsort() The function sorts the associative array in descending order by key name.
ksort() The function sorts the associative array in ascending order by key name.
This article is an introduction to the sorting method of associative arrays. I hope it will be helpful to friends in need!
The above is the detailed content of How to sort associative array in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!