We are learningThe elements in a general array are represented by characters or numbers, so you can arrange the elements in ascending order, which is the sort () function. Like what:
-
Php
- $ people = Array (' name ', ' Sex ', ' nation ', ' birth ');
- foreach ($people as $mychrs)
- echo $mychrs. " ";
- Sort ($people);
- echo " <BR />---sorted---<br />";
- foreach ($people as $mychrs)
- echo $mychrs. " ";
- ?>
The ascending sorted array elements are displayed as birth name Nation sex, of course, the PHP function sort () is case-sensitive (Letters from the largest to smallest order is: A ...). Z...A...Z)
The Sort () function also has a second argument that indicates whether the ascending rule is used to compare numbers or strings. Like what:
-
Php
- echo "---sorted in ascending order of numbers--- <BR />";
- $ num2 = Array (' 26 ', ' 3 ',);
- Sort ($num 2,sort_numeric);
- foreach ($num 2 as $mychrs)
- echo $mychrs. " ";
- echo " <BR />---sorted by word Fu Shen---<br /> ";
- $ num3 = Array (' 26 ', ' 3 ');
- Sort ($num 3,sort_string);
- foreach ($num 3 as $mychrs)
- echo $mychrs. " ";
- ?>
Sort_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 PHP function sort (), for example:
- $ NUM1 = Range (1,9);
http://www.bkjia.com/PHPjc/446405.html www.bkjia.com true http://www.bkjia.com/PHPjc/446405.html techarticle We are learning that each element in a general array is represented by a character or number, so it is possible to arrange the elements in ascending order, which is the sort () function. Like what:? PHP $ people = array ...