Home > Article > Backend Development > Learn php sorting function_PHP tutorial
Note: This function assigns a new key name to the unit in the array. The original key name will be deleted.
Returns TRUE if successful, otherwise returns FALSE.
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
sort($my_array);
print_r($my_array);
?>
output:
Array
(
[0] => Cat
[1] => Dog
[2] => Horse
)
asort() function sorts the array and maintains the index relationship. Mainly used for sorting associative arrays where the order of cells is important.
The optional second parameter contains additional sorting flags.
Returns TRUE if successful, otherwise returns FALSE.
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
asort($my_array);
print_r($my_array);
?>
output:
Array
(
[b] => Cat
[a] => Dog
[c] => Horse
)
The ksort() function sorts the array by key name, retaining the original keys for the array values.
The optional second parameter contains additional sorting flags. www.jbxue.com
Returns TRUE if successful, otherwise returns FALSE.
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
ksort ($my_array);
print_r($my_array);
?>
output:
Array
(
[a] => Dog
[b] => Cat
[c] => Horse
)
The difference between the above three functions is The key name, key value, and whether to change the index vary
>>> Articles you may be interested in: