- $a = Array (4, "37", 3,100,0,-5);
- Sort ($a);
- for ($i =0; $i <6; + + $i) {
- echo $a [$i]. " ";
- }
- echo "
";
- Sort ($a, sort_string);
- for ($i =0; $i <6; + + $i) {
- echo $a [$i]. " ";
- }
- echo "
";
- ?>
Copy CodeOutput Result:-5 0 3 4 37 100-5 0 100 3 37 4 Sort Descending: Rsort (array, [sort type]) parameter usage is the same as the sort function. Associative array Ordering: functions: Asort (array, [Sort type]) Description: Sort Ascending According to the element values of the associative array. The parameters are used in the sort function above. Function: Ksort (array, [Sort type]) Description: Sort Ascending According to the keyword of the associative array. The parameters are used in the sort function above.
$a = Array (
- "Good" = "bad",
- "Right" = "wrong",
- "Boy" = "girl");
echo "Value sort ";
- Asort ($a);
- foreach ($a as $key = = $value) {
- echo "$key: $value
";
- }
echo " Key sort ";
- Ksort ($a);
- foreach ($a as $key = = $value) {
- echo "$key: $value
";
- }
- ?>
Copy CodeOutput Result: Value sort Good:bad boy:girl right:wrong Key sort Boy:girl Good:bad right:wrong descending sort: arsort (array, [sort type]) corresponds to Asort (array, [Sort type]) with Krsort Corresponding Quickly create an array of functions range () For example, the range () function can quickly create arrays of numbers from 1 to 9:
- $numbers =range (1,9);
- echo $numbers [1];
- ?>
Copy CodeOf course, using range (9,1) creates an array of numbers from 9 to 1. Also, range () can create an array of characters from A to Z:
- $numbers =range (a,z);
- foreach ($numbers as $mychrs)
- echo $mychrs. " ";
- ?>
Copy CodeNote the case when using a character array, such as range (A,Z) and range (A,Z) are not the same. The range () function also has a third argument that sets the step size, such as the array elements created by range (1,9,3): 1, 4, 7. Common PHP array sorting the elements in a general array are represented by characters or numbers, so you can sort the elements in ascending order, the function is sort (). Like what:
- $people =array (' name ', ' Sex ', ' nation ', ' birth ');
- foreach ($people as $mychrs)
- echo $mychrs. " ";
- Sort ($people);
- echo "
After---sort--- ";
- foreach ($people as $mychrs)
- echo $mychrs. " ";
- ?>
Copy CodeAn ascending sorted array element is displayed as birth name Nation sex, of course, the sort () function is case-sensitive (Letters from the largest to smallest order are: A ... Z...A...Z) The sort () function also has a second parameter, which is used to indicate whether the rule in ascending order of the PHP array is to compare numbers or strings. Like what:
- echo "---sorted in ascending order of numbers---
";
- $num 2=array (' 26 ', ' 3 ',);
- Sort ($num 2,sort_numeric);
- foreach ($num 2 as $mychrs)
- echo $mychrs. " ";
- echo "
---ordered by word Fu Shen--- ";
- $num 3=array (' 26 ', ' 3 ');
- Sort ($num 3,sort_string);
- foreach ($num 3 as $mychrs)
- echo $mychrs. " ";
- ?>
Copy CodeSort_numeric and sort_string are used to declare in ascending order of numbers or characters. If the numbers are sorted in ascending order: 3, 26, but if ordered by word Fu Shen: 26, 3. In addition to the ascending function in PHP, there are functions in descending or reverse order, that is, the Rsort () function, such as: $num 1=range (1,9), Rsort ($num 1), which is actually equivalent to range (9,1). The contents of the ordering of the PHP array are finished, I wish you a happy study. >>> For more information, check out the PHP array sorting method Daquan <<< |