1. What is an array
An array is a special variable that can store multiple values in a single variable.
If we have a list of items (for example: a list of car names), store it in a single variable like this:
$cars1="Volvo";
$ cars2="BMW";
$cars3="Toyota";
However, what if we want to loop through the array and find a specific one? What if the array has not just 3 items but 300?
The solution is to create an array!
Arrays can store multiple values in a single variable, and at this time we can access the values according to the key. In the final analysis, the array is a combination of key-value pairs.
2. Creating arrays in PHP
In PHP, the array() function is used to create arrays:
array();
In PHP, there are three types of arrays:
Indexed arrays - arrays with numeric ID keys
Associative array - an array with specified keys, each key is associated with a value
Multidimensional array - an array containing one or more arrays (described in the following chapters )
3. PHP index array
There are two ways to create an index array:
1. Automatically assign ID (ID value automatically starts from zero)
$cars=array("Volvo","BMW","Toyota");
2. Manually assign ID (ID value is assigned by yourself, it does not need to start from zero, or it does not need to be continuous)
$cars[3]="Volvo";
$cars[6]="BMW";
$cars[8]="Toyota";
Example: The code is as follows
<?php //创建一个数组,并且输出一句欢迎词 $str=array("PHP.cn","学习","成长"); echo "大家好,欢迎来到".$str[0]."这个大家庭,以后大家一起".$str[1]."一起".$str[2] ?>
3. To introduce an array acquisition Length function - country()
count() function is used to return the length of the array (number of elements):
Example:
<?php //创建一个数组,并且输出一句欢迎词 $str=array("PHP.cn","学习","成长"); echo count($str); ?>
4. PHP associative array
## The
difference between an associative array and a numeric array is that the subscript of a numeric array (that is, the value of the key itself) can only be a number, while an associative array can be a string.
There are two ways to create it Associative array$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); Or:$age['Peter']="35";$age['Ben']="37";
$age['Joe']="43";
<?php //改造下之前的例子 //创建一个数组,并且输出一句欢迎词 $str=array("字符1"=>"PHP.cn","字符2"=>"学习","字符3"=>"成长"); echo "大家好,欢迎来到".$str["字符1"]."这个大家庭,以后大家一起".$str["字符2"]."一起".$str["字符3"]; ?>
5. Traverse the index and associative array
1. Traverse the index array
To traverse and print all values in the numeric array, you can use a for loop.
Example: The code is as follows
<?php //创建一个索引数组,并遍历输出 $str=array("PHP.cn","学习","成长"); $strlength=count($str); for($i=0;$i<$strlength;$i++){ echo $str[$i]; echo "<br/>"; } ?>
Note: Traversing the array means finding the elements in the array one by one and performing the corresponding operations
2. Traverse the associative array
Associative array subscripts are not numbers and cannot be output using a for loop, so we use foeeach to do this example
The code is as follows:
<?php //创建一个索引数组,并遍历输出 $str=array("字符1"=>"PHP.cn","字符2"=>"学习","字符3"=>"成长"); $strlength=count($str); foreach($str as $key=>$value){ echo $key."对应----".$value."<br/>"; } ?>Next Section