Php uses foreach to operate the array code. For more information, see. Foreach () has two usage methods:
The code is as follows:
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.
The code is as follows:
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:
The code is as follows:
/*-------------------------------------------------------------------------*/
/* Foreach example 1: value only */
Echo "foreach example 1: value only ".'
';
$ A = array (1, 2, 3, 17 );
Foreach ($ a as $ v ){
Echo "Current value of". $ a. ":". $ v ."
";
}
?>
// Running 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
The code is as follows:
/*-------------------------------------------------------------------------*/
/* 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
The code is as follows:
/*-------------------------------------------------------------------------*/
/* 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
The code is as follows:
/*-------------------------------------------------------------------------*/
/* Foreach example 4: multi-dimen1_arrays */
Echo'
'.'
'. "Foreach example 4: multi-dimen1_arrays ".'
';
$ A = array ();
$ A [0] [0] = "";
$ 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
The code is as follows:
/*-------------------------------------------------------------------------*/
/* 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:
The code is as follows:
$ MessageNav ['homepage'] = ROOT_PATH;
$ MessageNav ['talent communication'] = "#"
$ MessageNav ['dynamic column'] = "hragent/cn /"
$ Value):?>
"> >
"Class =" onlink ">