This article mainly gives you a detailed introduction of the PHP foreach () Usage and related examples, very meticulous, the need for small partners can refer to.
PHP 4 introduces a foreach structure, which is similar to Perl and other languages. This is just a simple way to traverse an array. foreach can only be used in arrays, and an error occurs when you try to use them for other data types or for an uninitialized variable. There are two types of syntax, the second is a useful extension that is minor but is the first.
?
1
2
3
4
5foreach (array_expression as $value)
Statement
foreach (array_expression as $key => $value)
Statement
The first form traverses the given array_expression array. In each loop, the value of the current cell is assigned to the $value and the pointer inside the array moves forward one step (so the next cell will be taken in the next loop).
The second format does the same thing, except that the key name of the current cell is assigned to the variable $key in each loop.
First look at the first statement, this statement is relatively simple, array_expression refers to an array expression, the as $val statement in order to get the value of the array and save to the $val variable, this method can only get the value in the array, but not the array of subscript index values. For example:
?
1
2
3
4$myarray=array ("1" => "Val1", "2" => "Val2", "3" => "Val3");
foreach ($myArray as $val) {
Print ($val. " ");
}
The result is output: Val1 val2 Val3
Then look at the second format, the second format, in addition to the same as the first format to get the value of the elements in the array, you can also get the index value of the element, and save to the $key variable, if the index value of the array has not been manually set, then return the system default settings,
See positive examples:
Let's look at a simple one-dimensional array:
?
1
2
3
4$myarray=array ("1" => "Val1", "2" = "Val2", "3" => "Val3");
foreach ($myArray as $key => $val) {
Print ($key. " => ". $val."; ");
}
The program will output: 1=>VAL1;2=>VAL2;3=>VAL3; Next, let's look at a more complex two-dimensional array traversal, which is as follows:
?
17$myarray=array (
"1" =>array ("one" => "Val11", "one" => "val12", "" => "Val13"),
"2" =>array ("=>" "Val21", "=>" "Val22", "" => "Val23"),
"3" =>array ("=>" "Val31", "=>" "Val32", "=>")
);
Print ("
");
foreach ($myArray as $key => $val) {
Print ("
- ". $key."
");
if (Is_array ($val)) {//To determine whether the value of the $val is an array, and if so, go to the lower traversal
Print ("
");
}
}
Print ("
");
Output results:
?
121
11=>val11
12=>val12
13=>val13
2
21=>val21
22=>val22
23=>val23
3
31=>val31
32=>val32
33=>val33
And
- is a label that displays a solid small dot and a hollow dot.
Because the above is a two-dimensional array, the first traversal of the $val value will be an array, so I added a judgment in the traversal, so that the two-tier array traversal.
One more example to dispel
?
$a = Array ("1" => "language", "2" => "Mathematics", "3" => "English");
$b = Array ("1" => "", "2" => "", "3" => "92");
foreach ($a as $key => $value) {
Echo $value;
echo $b [$key]. "
";
}
?>
The question is why is the value in the output array $b $b[$key] instead of $b[$value]?
What is this for?
$a = Array ("1" => "language", "2" => "Mathematics", "3" => "English");
This one is exactly the same as the one below.
$a [1]= "language";
$a [2]= "mathematics"
$a [3]= "English"
How do we output the array above?
Must be echo $a [1];
Right?
If there is no doubt, we continue to!!!!
------------------------------
Simply, foreach.
Its format is such a foreach (array name as subscript => value)
Subscript is the above $a[1], where 1 is the subscript of the array!
To this you should understand, why is $a[$key] so output
You remember that no matter how the array output method is always $a[1], will not be $a[' language '
================================================================
foreach () has two uses:
?
1
2
31:foreach (Array_Name as $value) {
Statement
}
The array_name here is the array name you want to traverse, each time the value of the current element of the Array_Name array is assigned to the $value, and the subscript inside the array is moved down one step, which is the next loop to get the next element.
?
1
2
32:foreach (array_name as $key => $value) {
Statement
}
The difference between this and the first method is the addition of a $key, in which the key value of the current element is assigned to the variable $key in every loop, except for assigning the value of the current element to $value. The key value can be either a subscript value or a string. For example, "0" in Book[0]=1, "id" in book[id]= "001".
The above mentioned is the entire content of this article, I hope you can enjoy.