PHP uses foreach to traverse arrays more efficiently. Foreach () can be used in two ways: foreach (array_nameas $ value) {statement;} here, array_name is the name of the array to be traversed, the current element value of the array_name array is assigned with foreach:
foreach(array_name as $value) { statement; }
Here, array_name is the name of the array to be traversed. in each loop, the value of the current element of the array_name array is assigned to $ value, and the downloading inside the array moves down one step, that is, the next element is returned in the next loop.
foreach(array_name as $key => $value) { statement; }
The difference between this method and the first method is that there is an extra $ key, that is, in addition to assigning the value of the current element to $ value, the key value of the current element is also assigned to the variable $ key in each loop. The key value can be either a lower mark value or a string. For example, "0" in book [0] = 1, "id" in book [id] = "001 ".
Program instance:
1
'; $ A = array (1, 2, 3, 17); foreach ($ a as $ v) {echo "Current value ". $. ":". $ v."
";}?> // Run the result foreach example 1: value only Current value of $ a: 1 Current value of $ a: 2 Current value of $ a: 3 Current value of $ a: 17
2
/* ----------------------------------------------------------------------- * // * Foreach example 2: value (with key printed for authentication) */echo'
'.'
'. "Foreach example 2: value (with key printed for authentication )".'
'; $ A = array (1, 2, 3, 17); $ I = 0;/* for each strative purposes only */foreach ($ a as $ v) {echo "" $ a [$ I] => $ v ".'
'; $ I ++;} // program running result foreach example 2: value (with key printed for authentication) $ a [0] => 1 $ a [1] => 2 $ a [2] => 3 $ a [3] => 17
3
/* ------------------------------------------------------------------------- * // * Foreach example 3: key and value */echo'
'.'
'. "Foreach example 3: key and value ".'
'; $ A = array ("one" => 1, "two" => 2, "three" => 3, "seventeen" => 17 ); foreach ($ a as $ k = >$ v) {echo "" $ a [$ k] => $ v ".'
';} // Program running result foreach example 3: key and value $ a [one] => 1 $ a [two] => 2 $ a [three] => 3 $ a [seventeen] => 17
4
/* ------------------------------------------------------------------------- * // * Foreach example 4: multi-dimen1_arrays */echo'
'.'
'. "Foreach example 4: multi-dimen1_arrays ".'
'; $ A = array (); $ a [0] [0] = "a"; $ a [0] [1] = "B "; $ a [1] [0] = "y"; $ a [1] [1] = "z"; foreach ($ a as $ v1) {foreach ($ v1 as $ v2) {echo "$ v2" n ";}}// program running result foreach example 4: multi-dimen1_arrays a B y z
5
/* ------------------------------------------------------------------------- * // * Foreach example 5: dynamic arrays */echo'
'.'
'. "Foreach example 5: dynamic arrays ".'
'; Foreach (array (1, 2, 3, 4, 5) as $ v) {echo "$ v" n ";}// program running result foreach example 5: dynamic arrays 1 2 3 4 5
This can also be used:
$ MessageNav ['homepage'] = ROOT_PATH; $ messageNav ['talent communication'] = "#" $ messageNav ['dynamic columns'] = "hragent/cn /"
$ Value):?>
">
>
"Class =" onlink ">
Http://www.bkjia.com/PHPjc/752574.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752574.htmlTechArticleforeach () has two Usage: foreach (array_name as $ value) {statement;} here the array_name is the name of the array you want to traverse, in each loop, the value of the current element of the array_name array is assigned...