An array of PHP development (i)
An array is a set of data that combines a series of data to form an actionable whole. An array of arrays is an ordered set of variables in which each variable is called an element. Each element is distinguished by a special identifier, which is called a key (also known as a subscript). Each entity in the array contains two items: A key and a value. The corresponding array element can be obtained by the key value.
There are two main ways to declare an array in PHP: One is to use the array () function to declare an array, and the other is to declare an array directly by assigning a value to the element of the array.
The method of declaring a function with array () is as follows:
Array ([mixed] ...)
The syntax for parameter mixed is key =>value, and multiple parameter mixed are separated by commas, and the indexes and values are defined separately. The index value can be a string or a number. If the index is omitted, an integer index starting from 0 is automatically generated.
Use Array () to declare the array code as follows:
$array = array("Jack","Bruce","hero");print_r($array);?>
The results of the operation are as follows:
one-dimensional arrays
When an element of an array is a variable, the array is called an array. A one-dimensional array is the most common array that saves only one column of content.
two-dimensional arrays
The element of an array, if it is a one-dimensional array, is said to be a two-dimensional array.
The following is a concrete example of declaring a two-dimensional array with the following code:
$array =array("老师"=>array("男老师","女老师"),"动物"=>array("哺乳动物","鸟类","鱼类"));print_r($array);?>
The results of the operation are as follows:
iterating through an array
Use the foreach structure to iterate through the array. The foreach structure does not operate on the array structure itself, but rather as a backup of the array.
The code to traverse the array with a foreach structure is as follows:
$array =array('jack'=>'jack is a teacher','Lisa'=>'Lisa is an actress','Fits'=>'Fits is a scientist');foreach($array as $value){echo $value."\n"; }?>
The results of the operation are as follows: