foreach ()
The PHP foreach () syntax structure is used to traverse an array of operations or output, and foreach () can only be used to traverse an array or object, and an error occurs when an attempt is made to use it for another data type or an uninitialized variable.
Grammar:
foreach (array as $value)
statement
//or:
foreach (array as $key => $value)
statement
In the syntax above, each loop assigns the value of the current cell to the $value and the pointer inside the array moves forward one step. The key name of the current cell is also assigned to the variable $key in each loop in the second syntax format.
Example:
<?php
$arr _age = Array ("Wang" =>18, "Li" =>20, "Zhang" =>25);
foreach ($arr _age as $age) {
echo $age, ' <br/> ';
}
? >
Run the example output:
Using array key values
<?php
$arr _age = Array ("Wang" =>18, "Li" =>20, "Zhang" =>25);
foreach ($arr _age as $key => $age) {
echo $key, ': ', $age, ' <br/> ';
}
? >
Run Example output:
When foreach starts executing, the pointer inside the array automatically points to the first cell, which means that reset () is not required before the Foreach loop.
foreach operates on a copy of the specified array, not the array itself. Modifications to the returned array cells do not affect the original array (see the example below), but the Foreach loop runs to the end, and the internal pointer to the original array points to the end of the array.
<?php
$arr _age = Array ("Wang" =>18, "Li" =>20, "Zhang" =>25);
foreach ($arr _age as $age) {
$age = $age +10;
echo $age, ' <br/> ';
}
Outputs the original array
print_r ($arr _age);
? >
Run Example output:
([Wang] => [Li] => [Zhang] => 25)
To modify the original array element in foreach, you can do so by reference, changing the example above to:
<?php
$arr _age = Array ("Wang" =>18, "Li" =>20, "Zhang" =>25);
foreach ($arr _age as & $age) {
$age = $age +10;
echo $age, ' <br/> ';
}
Outputs the original array
print_r ($arr _age);
? >
Run Example output:
([Wang] => [Li] => [Zhang] => 35)
Traversing multidimensional arrays
The foreach syntax structure can only be used to traverse a one-dimensional array, to traverse a multidimensional array, typically by using a foreach nested recursive or by dividing the original array into a one-dimensional array for a foreach traversal.
Example of one or two-D array blending:
<?php
$arr _age = Array ("Wang" =>18, "Li" =>20, "Zhang" =>array ("name" => "Xiao Zhang", "Age" =>25));
foreach ($arr _age as $age) {
if (Is_array ($age)) {
foreach ($age as $detail) {
echo $detail, ' <br/> ';
}
} else {
echo $age, ' <br/> ';
}
? >
The traversal processing of multidimensional array should be based on the actual data structure to take the most appropriate approach.
PHP arrays are implemented through the hash table (HashTable) tables, so foreach traverses the array based on the order in which the elements are added. If you want to traverse by index size, you should use a for () loop traversal.
for () looping through an array
If you are an array that operates on consecutive key values, you can also use a for () loop to traverse an array:
<?php
$arr _age = Array (a);
$num = count ($arr _age);
for ($i = 0; $i < $num; $i + +) {
echo $arr _age[$i]. " <br/> ";
}
? >
You can also use a combination of list () and each () to traverse an array, but the test finds less efficient than foreach ().
The above PHP array traversal foreach syntax structure and example is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.