1, quickly create an array of functions range ()
For example, the range () function can quickly create an array of numbers from 1 to 9:
Copy Code code as follows:
<?php
$numbers =range (1,9);
echo $numbers [1];
?>
Of course, using range (9,1) creates a 9 to 1 array of numbers. At the same time, range () can also create an array of characters from A to Z:
Copy Code code as follows:
<?php
$numbers =range (a,z);
foreach ($numbers as $mychrs)
echo $mychrs. " ";
?>
Note case when using a character array, such as range (A,Z) and range (A,Z) are different.
The range () function also has a third parameter, which is to set the step length, such as the array elements created by range (1,9,3) are: 1, 4, 7
Ordering of regular arrays in 2,php
The elements in a generic array are represented by characters or numbers, so you can sort the array elements in ascending order, the function is sort (). Like what:
Copy Code code as follows:
<?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 array elements that are sorted in ascending order are displayed as birth name Nation sex, and of course the sort () function is case-sensitive (letters from large to small) in the order that a ... Z...A...Z)
The Sort () function also has a second argument that shows whether ascending rules are used to compare numbers or strings. Like what:
Copy Code code as follows:
<?php
echo "---sorted by number in ascending order---<br/>";
$num 2=array (' 26 ', ' 3 ',);
Sort ($num 2,sort_numeric);
foreach ($num 2 as $mychrs)
echo $mychrs. " ";
echo "<br/>---sorted by word Fu Shen order---<br/>";
$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 ascending order by number or character. If you are in ascending order of numbers: 3, 26, but if you follow the word Fu Shen order is: 26, 3.
In addition to ascending functions in PHP, there is a descending or backward-ordered function, which is the Rsort () function, such as:
Copy Code code as follows:
$num 1=range (1,9);
Rsort ($num 1);
This is actually equivalent to range (9,1)
Ordering of associative arrays in 3,php
In addition to supporting numeric indexed arrays, PHP supports related 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 ');
Using sort ($peoples) defaults to sort by the ascending order of the element definition values, you can use the Asort () function in associative arrays to indicate that the most important of associative arrays is to sort by keywords (such as XM, XB, MZ, etc.) in ascending order, which is the function ksort () function.
Copy Code code as follows:
<?php
$peoples =array (' xm ' => ' name ', ' xb ' => ' sex ', ' mz ' => ' nation ', ' cs ' => ' birth ');
foreach ($peoples as $mychrs)
echo $mychrs. " ";
echo "<br/>--sorted by element value in ascending order--<br/>";
Asort ($peoples);
foreach ($peoples as $mychrs)
echo $mychrs. " ";
echo "<br/>--in ascending order of keywords--<br/>";
Ksort ($peoples);
foreach ($peoples as $mychrs)
echo $mychrs. " ";
?>
And the regular array has the reverse sort rsort () descending function of the sort () ascending function, the associative array also has the 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 indicates that associative array correlation must be predecessor, and the reverse sort uses the R modifier.
Random ordering of 4,php array elements
PHP uses the shuffle () function to randomly reorder the elements of an array, such as:
Copy Code code as follows:
<?php
$fer =array (' cnbruce ', ' cnrose ', ' cnjames ', ' Cnanne ');
Shuffle ($fer);
foreach ($fer as $mychrs)
echo $mychrs. " ";
?>
Different sorting combinations are displayed each time
5,php Array reverse sort in original order
You can use the Array_reverse () function in PHP to reverse the ordering of the array elements in their original order. Like what:
Copy Code code as follows:
<?php
$fer =array (' cnbruce ', ' cnrose ', ' cnjames ', ' Cnanne ');
foreach ($fer as $mychrs)
echo $mychrs. " ";
$fer =array_reverse ($fer);
echo "<br/>--reverse--<br/> in original order";
foreach ($fer as $mychrs)
echo $mychrs. " ";
?>
cnbruce cnrose cnjames Cnanne
-- reverse in original order--
Cnanne cnjames cnrose cnbruce
Note the $fer =array_reverse ($fer); Any sort function that precedes this is just declaring a reference and does not redefine the original array, but it needs to be redefined when I debug the function. This is because Array_reverse () is a modified copy of the original array, and if the original array is not needed, the original array can be redefined for overriding purposes, otherwise an array save copy is defined, for example: $fer _bak=array_reverse ($fer);