PHP sort array - array sort

 PHP sort function is used to array the way to reorder the array sort, sort in ascending function may be digital or letters of the alphabet size, i.e. from small to large numbers sorted alphabetically from A to Z, and the discharge will be capitalized Before lowercase, the PHP sort function sorts according to the array value, not the array key. The sorting algorithm used by the PHP sort function is the Quicksort algorithm, which is generally used in Sorting implementation of PHP Array .


PHP sort array array sort syntax

bool sort (array &$array, int $sort_flags = SORT_REGULAR)


The seemingly complicated sort function and parameters are actually not difficult. The first parameter array $array in parentheses is the array to be reordered. If you simply want to reorder the array in ascending powers, then Just put arrary into the sort function. The second parameter "int $sort_flags = SORT_REGULAR" is an optional item to change the sorting behavior of sort. If you don't need it, you don't need to fill it in.

PHP sort array array sorting example: numbers
<?php
 $NewArray = array("300","500","100","600","200","400");
 sort($NewArray);
 print_r($NewArray);
?>




The array $NewArray prepared at the beginning of the above output example is an array with many numeric array values. After sorting the chaotic numbers through PHP sort, you can easily output the neatly arranged numbers. This example does not use sort The second parameter "$sort_flags" of the function, in fact, the simple sort function is enough for normal use. The print_r at the end is the output array function.

PHP sort array array sorting example: English letters
<?php
 $NewArray = array("A","a","c","C","B","b");
 sort($NewArray);
 print_r($NewArray);
?>
The above output results



in this example, we first prepare an array $NewArray, in which there are 6 English letters, both upper and lower case. Re-sort the array in ascending powers through sort. You can see the uppercase letters from the output result They are all sorted in the front and rearranged in the order of A~Z, and the lowercase letters are in the back, the same result. It is worth noting that the array key still starts from zero, only the position of the array value is changed after sorting.

Note for using PHP sort array: Although sort array is very simple and easy to use in reordering, the Quicksort algorithm used is also very efficient, but sorting should be used with caution, because the process of rearranging will If the system resources are used up, if you don't pay attention to it when you use it, it is likely to happen without warning.

Post a Comment

0 Comments