asort()
Sort an array by its values from lowest to highest just like sort(). Only true or false is returned (success or failure). This function alters the input array itself.
The difference between this and sort() is that asort() keeps the keys intact. This is useful for associative arrays, but it keeps the numeric keys as well as string keys.
To sort from highest to lowest, see arsort() which works the same as this function.
Optional flags can be used as the second parameter. SORT_REGULAR is the default.
| SORT_REGULAR | Compare items normally (don’t change types) |
| SORT_NUMERIC | Compare items numerically |
| SORT_STRING | Compare items as strings |
| SORT_LOCALE_STRING | Compare items as strings, based on the current locale. |
If the values are of mixed types, you may not get the result you expect in some cases.
$arr = array('eggplant', 'banana', 'apple', 'dandelion', 'carrot'); asort($arr); /* array ( 2 => 'apple', 1 => 'banana', 4 => 'carrot', 3 => 'dandelion', 0 => 'eggplant' ) */ $arr = array('test' => 'eggplant', 'banana', 'sample' => 'apple', 17 => 'dandelion', 'carrot'); asort($arr); /* array ( 'sample' => 'apple', 0 => 'banana', 18 => 'carrot', 17 => 'dandelion', 'test' => 'eggplant' ) */ $arr = array('25', 43, 8004, 3, '8004', 0, -52); asort($arr); /* array ( 6 => -52, 5 => 0, 3 => 3, 0 => '25', 1 => 43, 4 => '8004', 2 => 8004 ) */ $arr = array('25', 43, 8004, 3, '8004', 0, -52); asort($arr, SORT_STRING); /* array ( 6 => -52, 5 => 0, 0 => '25', 3 => 3, 1 => 43, 2 => 8004, 4 => '8004' ) */
