Php development array (1) an array is a collection of data. it combines a series of data to form an operable whole. An array is a group of ordered variables, each of which is called an element. Each element is distinguished by a special identifier, which is called a key (also known as a subscript ). Each object in the array contains two items: Key and value. You can use the key value to obtain the corresponding array elements.
In php, there are two main methods to declare arrays: one is to use the array () function to declare arrays, and the other is to directly assign values to array elements to declare arrays.
The method to declare a function using array () is as follows:
Array ([mixed]…)
The syntax of the parameter mixed is key => value. multiple parameters are separated by commas (,), which define the index and value respectively. The index value can be a string or number. If the index is omitted, an integer index starting from 0 is automatically generated.
The following code uses array () to declare an array:
The running result is as follows:
One-dimensional array
When an array element is a variable, it is called an array. A one-dimensional array is the most common array, which stores only one column of content.
Two-dimensional array
If an array element is a one-dimensional array, it is called a two-dimensional array.
The following code uses a specific example to declare a two-dimensional array:
Array ("male", "female"), "animal" => array ("Mammals", "birds", "fish ")); print_r ($ array);?>
The running result is as follows:
Traverse arrays
Use the foreach structure to traverse the array. The foreach structure is not an operation on the array structure, but a backup operation on the array.
The code for traversing an array using the foreach structure is as follows:
'jack is a teacher', 'Lisa'=>'Lisa is an actress', 'Fits'=>'Fits is a scientist' ); foreach($array as $value){ echo $value."\n"; } ?>
The running result is as follows: