First, the definition of the array
An array is a language structure consisting of a key-value pair, similar to the hotel's room number, which is similar to what is stored in a hotel room. If you go to a hotel, the waiter will tell you how much room number, the specific room has stored what, it will need to enter the room according to the room number to know. The figure below is a clearer explanation of what an array is.
在PHP中,$arr = array();
Represents creating an empty array and assigning the created empty array to the variable $arr.
Second, indexed array initialization
PHP has two types of arrays: indexed arrays, associative arrays. Indexes and associations two words are for the keys of an array.
The index array is first introduced, the index array is an array of integers, and the key's integer order starts at 0, and so on. Here's a diagram to illustrate:
Can be implemented using the following code:
$fruit = array("苹果","香蕉","菠萝");
Note that the keys of the array start from 0. You can use print_r($fruit);
statements to output array keys and their corresponding values.
Three, indexed array assignment
There are three ways to assign an indexed array:
The first is to assign a value with the name of the array variable followed by a square bracket, and of course the key in the index array, in parentheses, must be an integer. Like what$arr[0]=‘苹果‘;
The second is to array()
create an empty array, use =>
symbols to separate the keys and values, the left side represents the key, and the right side represents the value. Of course, in an indexed array, the key must be an integer. Like whatarray(‘0‘=>‘苹果‘);
The third type: Use to array()
create an empty array, directly in the array in English single quotation marks ‘
or English double quotation marks "
, the array will default to establish a 0-based integer key. For example, array(‘苹果‘);
this array is equivalentarray(‘0‘=>‘苹果‘);
Iv. Accessing the contents of an indexed array
Sometimes we need to take the contents of the array as values out of the What to do? The values in the array are accessed by using the key in parentheses, followed by the name of the array variable.
For example: $fruit = Array (' Apple ', ' banana ');
$fruit 0 = $fruit [' 0 '];
Echo ($fruit 0);//Results for Apple
The function of the above code: first take an array $fruit first value and then assign to a variable $fruit0, the last output variable value.
V. For iterating through the values in an indexed array
The For loop can access all the values in the array, and below we show the values in the indexed array with a for loop.
For example:
$fruit =array (' Apple ', ' banana ', ' pineapple ');
for ($i =0; $i <3; $i + +)
{
echo ' <br> array '. $i. ' Value is: '. $fruit [$i];
}
Six, foreach iterates through the values in the indexed array
The Foreach loop can access all the values in the array, and we'll use a Foreach loop to iterate through the values in the indexed array.
For example:
$fruit =array (' Apple ', ' banana ', ' pineapple ');
foreach ($fruit as $k = $v)
{echo ' <br> '. $k. ' Value is: '. $v;}
In the output, you can also write {echo "{$k}--{$v}<br>";}, this is the first way for a foreach traversal of an indexed array, the second way:
foreach ($attr as $v)
{echo $v. " <br> ";}
An array of PHP basics