How to understand arrays
Array is a data type often used in php. It is widely used because of the characteristics of the php language. Variables can store multiple data of any type. Use data as a unit. An array is a collection of variables with the same value type. We can think of a variable as a box containing values, and then think of an array as a box with layers, each layer can store a value.
The purpose of using arrays
The most direct purpose of using arrays is to process data in batches. The essence of arrays is to store, manage and operate a set of variables. The capacity of an array to store data can be automatically adjusted according to the increase or decrease in the number of array elements. We can also use arrays to complete data structure functions in other strongly typed languages, such as collections in Java.
For example, we can declare the content in the table in the above figure using a composite type variable to form a two-dimensional array of "contacts". You can also use a double-layer loop to traverse each data in the two-dimensional array and output it to the browser. We can also insert the data into the database, or convert it into an xml file, etc.
In PHP, arrays can be divided into index arrays and associative arrays according to different ways of providing subscripts;
Index array: Index values in indexed arrays are integers. In most programming languages, arrays have numerical indexes, starting at 0 and incrementing one at a time. Array elements can be identified by position in an indexed array.
Associative array: Strings are used as index values in associative arrays. It is very rare in other programming languages, but it is very common in PHP to use strings as subscripted arrays. In associative arrays, array elements can be identified by name.
Method of array declaration
Direct assignment method declares an array. This is the simplest and most commonly used method of arrays. The syntax is as follows:
$Array variable name [index value] = content
The array has no size limit, and the array is initialized in one block at the same time of declaration. In the following example, two array variables are declared, namely person1 and person2. Use numbers in square brackets [] after the variable name to declare index arrays, and use strings to declare associative arrays. As shown below:
The output information is as follows:
Use the array() function to declare an array
array() language creates an array, and uses commas to separate the keys and values of the array (key=>value). The syntax format is as follows:
$Array variable name=array(key1 =>value1, key2=>value2....keyN=>valueN);
If "=>" is not used to match the specified subscript, the default is the index array . The default index value starts from 0 and increases one at a time. The code example is as follows:
$person1=array(1,"Zhang San","Male","Hebei");
The above code is created as a $person array, including four elements, and the default index value starts from 0.
Recommended tutorial: PHP video tutorial
The above is the detailed content of How to write php array. For more information, please follow other related articles on the PHP Chinese website!