One, what is an array
An array is a set of data that organizes a series of data into an operable whole. Each entity of an array contains two items: The key and the value.
II. Declaration of data
There are two main ways to declare an array in PHP:
One is to declare an array by applying the array () function,
One is to assign a value directly to an array element. Fei Fei asp! Technology Park
The <1>array () function declares an array of arrays ([mixed ...]) parameter mixed syntax is Key=>value
Such as
| The code is as follows |
Copy Code |
< PHP $array =array ("1" => "2″=>", "Cheng", "3″=>", "4″=>"); Print_r ($array); echo " “; echo $array [1]; Note: The subscript defaults to start at 0 Echo $array [2]; Echo $array [3]; Echo $array [4]; ?> |
<2> assigns values directly to an array element.
This is a good way to create an array if you don't know the size of the array you created, or if the size of the array might change when you actually write the program.
Such as
| The code is as follows |
Copy Code |
< PHP $array [1]= "I"; $array [2]= "Love"; $array [3]= "PHP"; Print_r ($array); Output the structure of the array created ?> |
Iii. type of array
PHP supports two arrays: an indexed array (indexed array) and a federated array (associative array), which uses a number as a key and a string as a key.
Four, output array
The array element output in PHP can be achieved by using the Echo and print statements, but this can only be output to an element in the group, and the Print_r () function is used to export the structure of the arrays, with the syntax: Print_r (mixed expression), Parameter expression is a normal shape, character, or real variable that outputs the variable itself, and if the argument is an array, all the elements in the array are displayed in the order of the key values and elements.
V. Construction of arrays
One-dimensional arrays:
When an element of an array is a variable, it is called a one-dimensional array. Fei Fei AS.P Technology Park
Declares an array: A type descriptor array name [constant expression];
Two-dimensional arrays:
When an array is an array of elements, it is called a two-dimensional array.
Such as
| The code is as follows |
Copy Code |
< PHP $str = Array ( "Network programming language" =>array ("PHP", "JSP", "ASP"), "Sports Project" =>array ("M" => "soccer", "N" => "basketball")); Print_r ($STR); ?> |
Six, traversing the array
Traversing all the elements in an array is a commonly used operation that can complete queries or other functions during traversal. There are a number of ways to traverse an array in PHP, and the two most commonly used methods are described below.
<1> traversing arrays using a foreach structure;
<2> use the list () function to traverse an array, and the list () function can only be used for arrays of numeric indices, and the numeric index starts at 0.
Example: Comprehensive use of List () and each () authenticated user login:
| The code is as follows |
Copy Code |
< PHP Output User Login information while (list ($name, $value) =each ($_post)) { if ($name!= "Submit") { echo "$name = $value “; } } ?> |
Statistics of the number of array elements
In PHP, the count () function is used to count the number of elements in the array, the syntax is: int coun (mixed array[,int mode]), where the parameter array is the necessary parameter, mode is an optional parameter, and if the Count--recursive is selected ( or 1), this function recursively arrays an array of pairs. Such as
| The code is as follows |
Copy Code |
< PHP $array = Array ("PHP" => Array ("PHP function Reference encyclopedia", "PHP Program Development Example Treasure", "PHP Database system Development Complete manual"), "ASP" => Array ("ASP experience Skill Treasure") ); Declare a two-dimensional array echo Count ($array, count_recursive);//recursive statistics the number of array elements, running with a result of 6 ?> |
Eight, array sorting
<1> use Sort () and rsort () to perform ascending and descending order of the array, for example,
| The code is as follows |
Copy Code |
< PHP $array =array (5,26,37,18,9,42,88,66); $array 1=sort ($array); For ($i =0 $i echo $array [$i]. " "; } echo " “; $array 1=rsort ($array); For ($i =0 $i echo $array [$i]. " "; } ?> Run 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 use an associated array, you want to keep the sort of keyword and value sorted after sorting, which requires the use of the Ksort () and Asort () letters
Number, such as,
| code is as follows |
copy code |
<? PHP $ Array=array (' php ' =>1, ' jsp ' =>2, ' ASP ' =>3); Ksort ($array); Print_r ($array); Echo ; Asort ($array); Print_r ($array); Run Result: Array ([ASP] => 3 [JSP] => 2 [PHP] => 1) Array ([PHP] => 1 [JSP] => 2 [ASP] => 3) |