- $ A = array (4, "37", 3,100, 0,-5 );
- Sort ($ );
- For ($ I = 0; $ I <6; ++ $ I ){
- Echo $ a [$ I]. "";
- }
- Echo"
";
- Sort ($ a, SORT_STRING );
- For ($ I = 0; $ I <6; ++ $ I ){
- Echo $ a [$ I]. "";
- }
- Echo"
";
- ?>
Output result:-5 0 3 4 37 100-5 0 100 3 37 4 Sort in descending order: rsort (array, [sort type]) parameters are used in the same way as sort functions. Sort joined arrays: Function: asort (array, [sort type]) description: sort joined arrays in ascending order. See the above sort function for parameters. Function: ksort (array, [sort type]) description: sort in ascending order based on the keywords of the associated array. See the above sort function for parameters.
$ A = array (
- "Good" => "bad ",
- "Right" => "wrong ",
- "Boy" => "girl ");
Echo "value sort ";
- Asort ($ );
- Foreach ($ a as $ key => $ value ){
- Echo "$ key: $ value
";
- }
Echo" Key sort ";
- Ksort ($ );
- Foreach ($ a as $ key => $ value ){
- Echo "$ key: $ value
";
- }
- ?>
Output result: value sort good: bad boy: girl right: wrong Key sort boy: girl good: bad right: wrong sort in descending order: arsort (array, [sort type]) corresponds to krsort (array, [sort type]) and ksort Quickly create an array function range () For example, the range () function can quickly create a number array from 1 to 9:
- $ Numbers = range (1, 9 );
- Echo $ numbers [1];
- ?>
Of course, range (9, 1) is used to create a number array from 9 to 1. At the same time, range () can also create a character array from a to z:
- $ Numbers = range (a, z );
- Foreach ($ numbers as $ mychrs)
- Echo $ mychrs ."";
- ?>
Note the case sensitivity when using character arrays. for example, range (A, z) and range (a, Z) are different. The range () function also has a third parameter, which is used to set the step size. for example, the array elements created by range (, 3) are: 1, 4, and 7. Common PHP array sorting: elements in an array are represented by characters or numbers. Therefore, you can sort array elements in ascending order. This function is sort (). For example:
- $ People = array ('name', 'sex', 'nation', 'birth ');
- Foreach ($ people as $ mychrs)
- Echo $ mychrs ."";
- Sort ($ people );
- Echo"
--- After sorting --- ";
- Foreach ($ people as $ mychrs)
- Echo $ mychrs ."";
- ?>
The array elements in ascending order are displayed as birth name nation sex. of course, the sort () function is case-sensitive (the order of letters from large to small is:... Z... A... Z) The Sort () function also has a second parameter, which indicates whether the PHP array sorting ascending rule is used to compare numbers or strings. For example:
- Echo "--- sort by numbers in ascending order ---
";
- $ Num2 = array ('26', '3 ',);
- Sort ($ num2, SORT_NUMERIC );
- Foreach ($ num2 as $ mychrs)
- Echo $ mychrs ."";
- Echo"
--- Sort by character in ascending order --- ";
- $ Num3 = array ('26', '3 ');
- Sort ($ num3, SORT_STRING );
- Foreach ($ num3 as $ mychrs)
- Echo $ mychrs ."";
- ?>
SORT_NUMERIC and SORT_STRING are used to declare the ascending order of numbers or characters. If the numbers are listed in ascending order: 3, 26, but if the characters are listed in ascending order, they are listed as 26, 3. In PHP, in addition to the ascending function, there are also descending or inverse sort functions, which are rsort () functions, such as: $ num1 = range (); rsort ($ num1 ); this is actually equivalent to range (9, 1 ). I have finished the introduction of php array sorting and wish you a pleasant learning experience. >>> For more information, see The php array sorting method < |