The arsort() in PHP is used to sort an array according to values. It sorts in a way that relation between indices and values is maintained. By default it sorts in descending order of values.
Syntax:
bool arsort( $array, $sorting_type )
Parameters: This function accepts two parameters as mentioned above and described below:
- $array: This parameter specifies the array which to be sort. It is a mandatory parameter.
- $sorting_type: This parameter specifies name of a user-defined function which will be used to sort the keys of array $array. This comparison function must return an integer.
Return Value: This function returns True on success or False on failure.
Below programs illustrate the arsort() function in PHP.
Program 1:
<?php // PHP program to illustrate // arsort() function // Input different array elements $arr = array("0" => "GeeksforGeeks", "1" => "Practice", "2" => "Contribute", "3" => "Java", "4" => "Videos", "5" => "Report Bug", "6" => "Article", "7" => "Sudo Placement" ); // Implementation of arsort() arsort($arr); // for-Loop for displaying result foreach ($arr as $key => $val) { echo "[$key] = $val"; echo"\n"; } ?> |
[4] = Videos [7] = Sudo Placement [5] = Report Bug [1] = Practice [3] = Java [0] = GeeksforGeeks [2] = Contribute [6] = Article
Program 2:
<?php // PHP program to illustrate // arsort() function // Input different array elements $arr = array("a" => 11, "b" => 22, "d" => 33, "n" => 44, "o" => 55, "p" => 66, "p" => 77, "q" => 88, ); // Implementation of arsort() arsort($arr); // for-Loop for displaying result foreach ($arr as $key => $val) { echo "[$key] = $val"; echo"\n"; } ?> |
[q] = 88 [p] = 77 [o] = 55 [n] = 44 [d] = 33 [b] = 22 [a] = 11
Related Articles:
Reference: http://php.net/manual/en/function.arsort.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to Check a Function is a Generator Function or not using JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- p5.js | nfs() Function
- p5.js | nfp() Function
- PHP | tan( ) Function
- D3.js | d3.lab() Function
- CSS | hsl() Function
- PHP | Ds\Set contains() Function
- PHP | exp() Function
- D3.js | d3.hcl() Function
- PHP | dir() Function
- D3.js | d3.max() function
- CSS | rgb() Function
- PHP | Ds\Set add() Function
- PHP | Ds\Set last() Function
- PHP | Ds\Set first() Function
- D3.js | d3.min() function
- D3.js | d3.map.set() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ManasChhabra2

