sort--to sort the Val of an array
ksort--to sort the key values of an array
asort--the array, the corresponding relationship between the key and the value is unchanged
1. Sort array
The format is as follows: bool Sort (array & $array [, int $sort _flags])
After using this function, the unit will be rescheduled from lowest to highest.
Returns a Boolean
$sort _flags This parameter is added in PhP4, you can change the ordering behavior with the following values
Sort type tag:
sort_regular-Normal comparison unit (does not change type)
The sort_numeric-unit is compared as a number.
The sort_string-unit is compared as a string.
sort_locale_string-the cell as a string comparison based on the current locale setting. php4.4.0 and 5.0.2 new addition. In
Before PHP6, the system's locale was used, which can be changed with setlocale (). Starting from PHP6, the I18n_loc_set_default () function must be used
Illustrate the use of sort
$array = Array (' 23 ', ' 96 ', ' 12 ', ' 50 ', ' 3 ');
Sort ($array);
foreach ($array as $key = = $val) {
echo "array[". $key. "] = ". $val." /n ";
}
2. ksort-array sorted by key name
The format is as follows: BOOL Ksort (Array & $array [, int $sort _flags])
The array is sorted by key name, preserving the association of the key name to the data.
Returns a Boolean
$sort _flags parameters are used like sort
Illustrate the use of ksort
$fruits = Array ("d" = "lemon", "a" and "Orange", "b" = "banana", "c" = "apple");
Ksort ($fruits);
foreach ($fruits as $key = = $val) {
echo "$key = $val/n";
}
Output
A = Orange
b = Banana
c = Apple
D = Lemon
3. asort-the array and keep the index relationship
The format is as follows: BOOL Asort (Array & $array [, int $sort _flags])
This function sorts the arrays, the index of the array remains, and the cell is associated. Used primarily to sort the associative arrays that are important for the cell order
Returns a Boolean value
$sort _flags parameters are used like sort
Illustrate the use of ksort
$fruits = Array ("d" = "lemon", "a" and "Orange", "b" = "banana", "c" = "apple");
Ksort ($fruits);
foreach ($fruits as $key = = $val) {
echo "$key = $val/n";
}
Output
c = Apple
b = Banana
D = Lemon
A = Orange
The difference between sort,ksort,asort