Array can store multiple different types of data and is a composite data type.
We can use array() to initialize the array, and use commas to separate each array element.
After the PHP5.4 version, the initialization of the array is more concise. Use it in Brackets [] to initialize
Let’s create a simple array:
<?php header("Content-type:text/html;charset=utf-8"); //设置编码 $array=array(100,1.34,true,"PHP中文网",); echo '<pre>'; var_dump($array); echo '<pre/>'; ?>
In the above example, we found that we stored:
1. Integer type
2. Floating point
3. Boolean
4. String
Note: The main purpose of the echo pre tag in the above example is to output it as it is, and the format will be displayed better ,clearer.
Let’s take a look at the results of running the program:
1. array(size = 4) means there are 4 elements in it
2. 0 => int 100 We know that int means integer, and 100 is an integer value. So what do the previous 0, 1, 2, 3 and => mean?
3. The latest 0, 1, 2, and 3 represent the read identification number of the value, which we call the subscript or key (English: key)
4. = > is a standard name for a symbol called: key-value correspondent. Therefore, later seeing 0=> int 1 can be said like this. The subscript accessor 0 corresponds to 1 of the integer type.
5. We also call the key-value pairs in the array elements, and elements are combinations of key-value pairs.
Through the above example, we have completed one of the declaration methods of arrays: Declaration of index array. The so-called index array: it is an array whose subscripts are all integers.
Little knowledge
The subscript of the index array must start from 0 Start?
Answer: This question is actually not true. The index array does not necessarily start from 0.
How can we not start from 0?
Answer: You need to use a small piece of knowledge you learned above. It’s the key-value correspondent. As follows
<?php header("Content-type:text/html;charset=utf-8"); //设置编码 $kele = array(5=>'只有不断努力才能博得未来',10 => 'php.cn', 'PHP中文网' ,'好好学习哦', 19 => '去PHP中文网学习PHP'); //打印显示$kele echo '<pre>'; var_dump($kele); echo '</pre>'; ?>
As can be seen from the above example, our index array subscript starts from 5
Let’s summarize the rules:
If the index array does not forcefully declare its subscript, its subscript starts from 0. (The value of our first array: Only through continuous efforts can we win the future. The subscript of this value is 0).
2. If I have specified a subscript, his subscript will be the value I specified. For example, the subscripts 5, 10 and 19 are all values I have specified.
3. If a certain value (such as php.cn) is forced to specify a subscript (the subscript is 10). The value added after it (PHP Academy), if no subscript is specified. Their subscripts increase
to the maximum value +1.
##Associative array
Example
<?php $arr=[ //用[]创建数组 'name'=>'Tom', 'age'=>'18', 'gender'=>'male' ]; echo "<pre>"; var_dump($arr) ; echo"</pre>" ?>
##We know through experiments:
1 . Declaring that the associative array is the key name => value
2. The associative array can have elements of the index array
Array is the most commonly used type, so how to calculate a certain The number of one-dimensional arrays. In fact, we can use one of the mathematical functions we learned before:.
count($variable) function is used to return the length of the array (number of elements ):
Example<?php
$name=Array('tom','andy','jack');
echo count($name);
?>
Traversing the two words, literally means reading and accessing them all one after another and displaying them.
<?php
$arr=Array('tom','andy','jack');
$arrlength=count($arr);
for($x=0;$x<$arrlength;$x++){
echo $arr[$x];
echo "<br/>";
}
?>
##Through the above example, we loop the array .
Because the subscript starts from 0, define $i=0. Let $i increase by 1 each time it loops, but it must be less than 3, because the maximum value of the array subscript is 2.In this way, we have learned to traverse the indexed consecutive subscript array.
What about the associative array? What if the subscripts of the index array are not consecutive? This will use the knowledge we have learned below.
Basic syntax of foreach
There is a Boolean loop that is specially used to loop arrays. The basic syntax of this loop is the basic syntax of foreach
foreach( The array variable to be looped as [key variable=>] value variable){
//The content of the loop
}
This is a fixed usage, put the array to be looped in .
as is a fixed keyword. The key variable after
is optional. You can define a variable at will. Every time it loops, the foreach syntax will Take out the key and assign it to the key variable. The value variable following
is required. Each time it loops, the value is placed in the value variable.
Let’s use code as an example to enhance our understanding of this syntax.
Example
?php $arr=[ 'name'=>'Tom', 'age'=>'18', 'gender'=>'male' ]; foreach($arr as $key=>$value){ echo $key.": ".$value."<br />"; }; ?>
Through the above running results, we get the following results:
1. Each time you loop, assign the subscript to the variable $key, and assign the value variable to the variable $value
2. The loop reads the key and value once. After reading to the end and finding that there are no array elements that can be read, the loop stops traversing the data.
Note: $key and $value do not have to be the variable names. You can also name it something else, such as $kai => $wen is the same. You need to know which variable the key is assigned
to, and which other variable the value is assigned to.
Traverse the continuous index array through foreach, as in the following example:
<?php header("Content-type:text/html;charset=utf-8"); //设置编码 $data = array( 0 => '中国', 100 => '美国', 20 => '韩国', 300 => '德国', ); foreach($data as $k => $v){ echo $k . '------' . $v .'<br />'; } ?>
The inference based on the result of foreach is the same as the result of the associative array just done.
The difference is the discontinuous index array. Each time an element of the array is read, the subscript of the current loop is assigned to the variable $k, and the value is assigned to the variable $v. The key and value are input
for each read and then displayed. The loop moves backward one index at a time. Read to the end and exit execution.
Multidimensional Array
We will discuss this in PHP Advanced Tutorial Explain multidimensional arrays in.
Complete PHP Array Reference Manual
For a complete reference manual for array functions, please visit our PHP Array Reference Manual.
This reference manual contains a brief description and usage examples of each function.