1, quickly create an array of function range ()
For example, the range () function can quickly create arrays of numbers from 1 to 9:
Copy CodeThe code is as follows:
$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:
Copy CodeThe code is as follows:
$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 element created by range (1,9,3): 1, 4, 7
ordering of regular arrays in 2,php
The 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:
Copy CodeThe code is as follows:
$people =array (' name ', ' Sex ', ' nation ', ' birth ');
foreach ($people as $mychrs)
echo $mychrs. " ";
Sort ($people);
echo "
After---sort---
";
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 argument that indicates whether the ascending rule is used to compare numbers or strings. Like what:
Copy CodeThe code is as follows:
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. " ";
?>
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:
Copy CodeThe code is as follows:
$num 1=range (1,9);
Rsort ($num 1);
This is actually the equivalent of range (9,1)
Ordering of associative arrays in
3,php
PHP supports associative arrays in addition to numeric indexed arrays. For example, the following array is a related (associative) array:
copy code code as follows:
$peoples =array (' xm ' = ' name ', ' xb ' = ' sex ', ' mz ' = ' nation ', ' cs ' = ' birth '); The
uses the sort ($peoples) by default to sort by the ascending order of the element-defined values, which can be represented by the Asort () function in the associative array, and the most important of the associative array is the ascending order of the keywords (such as XM, XB, MZ, etc.), which is ksort with the function ( function
copy code code is as follows:
!--? PHP $ Peoples=array (' xm ' = ' ' name ', ' xb ' = ' sex ', ' mz ' = ' nation ', ' cs ' = ' birth ');
foreach ($peoples as $mychrs)
Echo $mychrs. ";
Echo
--in ascending order of element values--
";
Asort ($peoples);
foreach ($peoples as $mychrs)
Echo $mychrs. ";
Echo
--in ascending order of keywords--
";
Ksort ($peoples);
foreach ($peoples as $mychrs)
Echo $mychrs. ";
?
and regular arrays have the sort () ascending function of the inverse sort rsort () corresponding to the descending function, the associative array also has a corresponding descending function: the Asort () function and the Arsort () function, the Ksort () function, and the Krsort () function.
Memory: The prototype function is sort (), where a, k means that associative array correlation must be pre-ordered, and reverse-sort using R-modifiers.
4,php array element random ordering
PHP uses the shuffle () function to randomly reorder array elements, such as:
copy code code is as follows:
!--? php $fer =array (' cnbruce ', ' cnrose ', ' cnjames ', ' Cnanne ');
Shuffle ($fer);
foreach ($fer as $mychrs)
Echo $mychrs. ";
?
Displays a different sort combination each time
5,php arrays are reversed in the original order
In PHP, you can use the Array_reverse () function to reverse the order of the array elements in the original sequence. Like what:
Copy the Code code as follows:
$fer =array (' cnbruce ', ' cnrose ', ' cnjames ', ' Cnanne ');
foreach ($fer as $mychrs)
echo $mychrs. " ";
$fer =array_reverse ($fer);
echo "
--Reverse in the original order--
";
foreach ($fer as $mychrs)
echo $mychrs. " ";
?>
Cnbruce cnrose cnjames Cnanne
--Reverse in the original order--
Cnanne cnjames Cnrose Cnbruce
Note here the $fer =array_reverse ($fer); Any sort function prior to this is simply declaring the reference and not redefining the original array, but I need to redefine it when I debug the function. This is because Array_reverse () is returning the modified copy of the original array, if the original array is not needed, the original array can be redefined to achieve the purpose of overwriting, otherwise define the array to save the copy, for example: $fer _bak=array_reverse ($fer);
http://www.bkjia.com/PHPjc/327752.html www.bkjia.com true http://www.bkjia.com/PHPjc/327752.html techarticle 1, quickly create an array of functions range () such as the range () function can quickly create arrays of numbers from 1 to 9: Copy code as follows: PHP $numbers =range (1,9), Echo $numbers [1];