English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The asort() function sorts an array while maintaining the index relationship
asort(array &$array[, int $sort_flags = SORT_REGULAR]);
This function sorts the array while keeping the index relationship between the array and its elements. It is mainly used to sort combined arrays where the order of the elements is very important.
Returns TRUE on success, or FALSE on failure.
Number | Parameters and Description |
---|---|
1 | array (required) It specifies an array. |
2 | sort_flags (optional) You can change the sorting method using the optional parameter sort_flags |
The array fruits is sorted alphabetically, and the index relationship of the elements remains unchanged.
<?php $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); asort($fruits); foreach ($fruits as $key => $val) { echo "$key = $val\n"; } ?>Test to see‹/›
Output Result:
c = apple b = banana d = lemon a = orange