With the rapid development of PHP, more and more people with it, in the PHP array learning excerpt part of the basic PHP array to understand the establishment and the display of the group elements. Need to learn more about the operation of the PHP array. The first contact is the PHP array sorting, descending sort problem.
Quickly create an array of functions range ()
- For example, the range () function can quickly create arrays of numbers from 1 to 9:
- Php
- $ numbers = Range (1,9);
- echo $numbers [1];
- ?>
Of 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:
- Php
- $ numbers = Range (a,z);
- foreach ($numbers as $mychrs)
- echo $mychrs. " ";
- ?>
Note 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:
- 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. " ";
- ?>
An 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:
- 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 Rsort () function, such as: $num 1=range (1,9), Rsort ($num 1), which is actually equivalent to range (9,1).
http://www.bkjia.com/PHPjc/446476.html www.bkjia.com true http://www.bkjia.com/PHPjc/446476.html techarticle with the rapid development of PHP, more and more people with it, in the PHP array learning excerpt part of the basic PHP array to understand the establishment and the display of the group elements. Need to drill down into the PHP array ...