An array is data that organizes a series of data to form an operational whole. Each entity in the array contains two keys and values.
First, what is the array Array is data, a series of data organized together to form an operational whole. Each entity in the array contains two keys and values.
Second, the statement data There are two main ways to declare an array in PHP:
First, the array () function declaration array,
First, directly to the array element assignment. Feifei Asp! Technology Paradise
<1> Array () method of array declaration array ([mixed ...]) The syntax for parameter mixed is key => value
Such as,
<? php
$ array = array ("1" => "", "2" = "", "3" => "word", "4" => "canon");
print_r ($ array);
echo "<br>";
Echo $ array [1]; / / Note: The subscript defaults to 0
echo $ array [2];
echo $ array [3];
echo $ array [4];
?>
<2> Assign values directly to array elements.
If you do not know the size of the array created when creating the array, or the size of the array may change when the program is actually being written, it is better to create it using this array.
Such as,
<? php
$ array [1] = "I";
$ array [2] = "love";
$ array [3] = "PHP";
print_r ($ array); // Outputs the structure of the array created
?>
Third, the type of array
PHP supports two arrays: indexed array and associative array, the former using numbers as keys and the latter using strings as keys.
Fourth, the output array The output of the array elements in PHP, you can echo and print statements to achieve, but this can only be an array of elements to be output; to the array structure you want to use print_r () function, the syntax: print_r (mixed expression), the parameter expression is an ordinary plastic, and the character or real variable outputs the variable itself. If the parameter is an array, all the elements in the array are displayed in a certain key and element order.
Fifth, the structure of the array One-dimensional array:
When an element of an array is a variable, it is called a one-dimensional array. Feifei As.p technology park statement an array: type specifier array name [constant expression];
Two-dimensional array:
When an element of an array is a one-bit array, it is called a two-dimensional array.
Such as,
<? php
$ str = array (
Network Programming Language => array ("PHP", "JSP", "ASP"),
"Sports" => array ("m" => "football", "n" => "basketball"));
print_r ($ str);
?>
Sixth, the traversal array Traversing all the elements in the array is a commonly used operation, in the traversal process can complete the query or other functions. There are several ways to traverse an array in PHP. Here are two of the most commonly used methods.
<1> Use foreach structure to traverse the array;
<2> To traverse an array using the list () function, the list () function can only be used for numeric indexed arrays, and the numeric index starts at zero.
Example: Comprehensive list () and each () Authentication User Login:
<? php
// Output user login information
while (list ($ name, $ value) = each ($ _ POST)) {
if ($ name! = "submit") {
echo "$ name = $ value <br>";
}
}
?>
Seven, the number of elements in the statistics array In PHP, the use of count () function to count the number of elements in the array, the syntax is: int coun (mixed array [, int mode]), where the parameter array is an essential parameter mode Optional parameters, if you select COUNT - RECURSIVE (or 1), the function will recursively array array. Such as,
<? php
$ array = array ("PHP" => array ("PHP Function Reference Daquan", "PHP Programming Paradigm", "PHP Database System Development Complete Manual"),
"asp" => array ("ASP experience skills Collection")
); // Declare a two-dimensional array
echo count ($ array, COUNT_RECURSIVE); / / recursive statistics the number of elements in the array, the result is 6
?>
Eight, array sorting
<1> Use sort () and rsort () to sort the array in ascending and descending order, respectively,
<? php
$ array = array (5,26,37,18,9,42,88,66);
$ array1 = sort ($ array);
for ($ i = 0; $ i <count ($ array); $ i ++) {
echo $ array [$ i]. "";
}
echo "<br>";
$ array1 = rsort ($ array);
for ($ i = 0; $ i <count ($ array); $ i ++) {
echo $ array [$ i]. "";
}
?>
operation result:
5 9 18 26 37 42 66 88
88 66 42 37 26 18 9 5
<2> Sort associative arrays using ksort () and asort () If you are using an associative array, keep the sort of keywords and values in the same order after sorting, which requires the use of ksort () and asort ,
<? php
$ array = array ('php' => 1, 'jsp' => 2, 'asp' => 3);
ksort ($ array);
print_r ($ array);
echo "<br>";
asort ($ array);
print_r ($ array);
?>
operation result:
Array ([asp] => 3 [jsp] => 2 [php] => 1)
Array ([php] => 1 [jsp] => 2 [asp] => 3)