foreach() has two uses:
Copy code The code is as follows:
foreach(array_name as $value)
{
statement;
}
The array_name here 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 to $value, and the array The internal subscript moves down one step, that is, the next loop returns to the next element.
Copy code The code is as follows:
foreach(array_name as $key => $value)
{
statement;
}
The difference between this and the first method is that there is an additional $key, that is, in addition to assigning the value of the current element to $value, the key value of the current element It will also be assigned to the variable $key in each loop. The key value can be a subscript value or a string. For example, "0" in book[0]=1, "id" in book[id]="001".
Program Example 1:
Copy code 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 results
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
Copy code The code is as follows:
/*-------------------------------------------------- --------------------------*/
/* foreach example 2: value (with key printed for illustration) */
echo '
'.'
'."foreach example 2: value (with key printed for illustration) ".'
';
$a = array (1, 2, 3, 17);
$i = 0; /* for illustrative purposes only */
foreach ($a as $v) {
echo ""$a[$i] => $v".'
';
$i++;
}
// Program execution result
foreach example 2: value (with key printed for illustration)
$a[0] => 1
$a[1] => 2
$a[2] => 3
$a[3] => 17
3
Copy code 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 execution result
foreach example 3: key and value
$a[ one] => 1
$a[two] => 2
$a[three] => 3
$a[seventeen] => 17
4
Copy code The code is as follows:
/*-------------------------- -------------------------------------------------- ---------*/
/* foreach example 4: multi-dimensional arrays */
echo '
'.'
'."foreach example 4: multi-dimensional 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-dimensional arrays
a b y z
5
Copy code 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 execution result
foreach example 5: dynamic arrays
1 2 3 4 5
It can also be used like this:
Copy the code The code is as follows:
$messageNav['Homepage'] =ROOT_PATH;
$messageNav['Talent Exchange'] ="#"
$messageNav['Dynamic Column'] ="hragent/cn/"
$value):?>
=$key?>>
=$key?>
http://www.bkjia.com/PHPjc/324013.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324013.htmlTechArticleforeach() has two uses: Copy the code The code is as follows: foreach(array_name as $value) { statement; } The array_name here is the name of the array you want to traverse. In each loop, the array_name array...