PHP | asort() Function
The asort() function is an inbuilt function in PHP which 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 ascending order of values.
Syntax:
bool asort( $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 is an optional parameter. There are different sorting types which are discussed below:
- SORT_REGULAR: The value of $sorting_type is SORT_REGULAR then items are compare normally.
- SORT_NUMERIC: The value of $sorting_type is SORT_NUMERIC then items are compares numerically.
- SORT_STRING: The value of $sorting_type is SORT_STRING then items are compares as string.
- SORT_LOCALE_STRING: The value of $sorting_type is SORT_STRING then items are compares as string, based on current locale.
Return Value: This function returns True on success or False on failure.
Below programs illustrate the asort() function in PHP.
Program 1:
<?php // PHP program to ilustrate // asort() function // Input differet array elements $arr = array("0" => "Web Technology", "1" => "Machine Learing", "2" => "GeeksforGeeks", "3" => "Computer Graphics", "4" => "Videos", "5" => "Report Bug", "6" => "Article", "7" => "Sudo Placement", "8" => "SContribute", "9" => "Reset", "10" => "Copy", "11" => "IDE", "12" => "Gate Note", ); // Implementation of asort() asort($arr); // for-Loop for displaying result foreach ($arr as $key => $val) { echo "[$key] = $val"; echo"\n"; } ?> |
[6] = Article [3] = Computer Graphics [10] = Copy [12] = Gate Note [2] = GeeksforGeeks [11] = IDE [1] = Machine Learing [5] = Report Bug [9] = Reset [8] = SContribute [7] = Sudo Placement [4] = Videos [0] = Web Technology
Program 2:
<?php // PHP program to ilustrate // asort() function // Input differet array elements $arr = array("a" => 11, "b" => 22, "d" => 33, "n" => 44, "o" => 55, "p" => 66, "r" => 77, "s" => 2, "q" => -11, "t" => 3, "u" => 1000, "z" => 1, ); // Implementation of asort() asort($arr); // for-Loop for displaying result foreach ($arr as $key => $val) { echo "[$key] = $val"; echo"\n"; } ?> |
[q] = -11 [z] = 1 [s] = 2 [t] = 3 [a] = 11 [b] = 22 [d] = 33 [n] = 44 [o] = 55 [p] = 66 [r] = 77 [u] = 1000
Related Articles:
Reference: http://php.net/manual/en/function.asort.php
Recommended Posts:
- ArrayObject asort() Function in PHP
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- PHP | Ds\Map first() Function
- p5.js | hex() function
- D3.js | d3.rgb() Function
- PHP | end() Function
- CSS | hsl() Function
- D3.js | d3.hcl() Function
- D3.js | d3.lab() Function
- PHP | pow( ) Function
- PHP | next() Function
- PHP | cos( ) Function
- PHP | dir() 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.



