We are learningGenerally, all elements in an array are represented by characters or numbers. Therefore, you can sort the array elements in ascending order. This function is sort (). For example:
- <?Php
- $People=Array('Name', 'sex', 'nation', 'birth ');
- Foreach ($ people as $ mychrs)
- Echo $ mychrs ."";
- Sort ($ people );
- Echo"<Br />--- After sorting ---<Br />";
- Foreach ($ people as $ mychrs)
- Echo $ mychrs ."";
- ?>
The array elements in ascending order are displayed as birth name nation sex. Of course, the PHP function sort () is case-sensitive (the order of letters from large to small is:... Z... A... Z)
The Sort () function also has a second parameter. The ascending rule is used to compare numbers and strings. For example:
- <?Php
- Echo "--- sort by numbers in ascending order ---<Br />";
- $Num2=Array('26', '3 ',);
- Sort ($ num2, SORT_NUMERIC );
- Foreach ($ num2 as $ mychrs)
- Echo $ mychrs ."";
-
- Echo"<Br />--- Sort by character in ascending order ---<Br />";
- $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 addition to the ascending function, PHP also has a descending or reverse sort function, which is the PHP function sort (). For example:
- $Num1=Range(1, 9 );
- Rsort ($ num1); // here it is actually equivalent to range (9, 1)