国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

PHP development basic tutorial array

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
//創(chuàng)建一個(gè)數(shù)組,并且輸出一句歡迎詞
$str=array("PHP.cn","學(xué)習(xí)","成長");
echo "大家好,歡迎來到".$str[0]."這個(gè)大家庭,以后大家一起".$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
//創(chuàng)建一個(gè)數(shù)組,并且輸出一句歡迎詞
$str=array("PHP.cn","學(xué)習(xí)","成長");
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";

The specified key can then be used in the script

Example: The code is as follows

<?php
//改造下之前的例子
//創(chuàng)建一個(gè)數(shù)組,并且輸出一句歡迎詞
$str=array("字符1"=>"PHP.cn","字符2"=>"學(xué)習(xí)","字符3"=>"成長");
echo "大家好,歡迎來到".$str["字符1"]."這個(gè)大家庭,以后大家一起".$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
//創(chuàng)建一個(gè)索引數(shù)組,并遍歷輸出
$str=array("PHP.cn","學(xué)習(xí)","成長");
$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
//創(chuàng)建一個(gè)索引數(shù)組,并遍歷輸出
$str=array("字符1"=>"PHP.cn","字符2"=>"學(xué)習(xí)","字符3"=>"成長");
$strlength=count($str);
foreach($str as $key=>$value){
	echo $key."對應(yīng)----".$value."<br/>";
}
?>
Continuing Learning
||
<?php //創(chuàng)建一個(gè)數(shù)組,并且輸出一句歡迎詞 $str=array("PHP.cn","學(xué)習(xí)","成長"); echo "大家好,歡迎來到".$str[0]."這個(gè)大家庭,以后大家一起".$str[1]."一起".$str[2] ?>
submitReset Code