这篇文章主要为大家详细介绍了PHP foreach循环用法示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编小韵来看看吧!
foreach基本语法如下:代码如下:
$array1 = array(1,2,3,4,5);
FOREACH ($array1 as $abc)
{
print "new value is " . $abc*10 . "<br>";
}
代码如下:
<?php
//四海网网 Www.q1010.com
/* foreach example 1: value only */
$a = array(1, 2, 3, 17);
foreach ($a as $v) {
echo "Current value of \$a: $v.\n";
}
/* 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.\n";
$i++;
}
/* 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.\n";
}
/* 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";
}
}
/* foreach example 5: dynamic arrays */
foreach (array(1, 2, 3, 4, 5) as $v) {
echo "$v\n";
}
?>
代码如下:
$s = array(array(1,2),array(3,4),array(5,6));
foreach( $s as $v => $_v )
{
foreach( $_v as $vc => $_vc )
{
echo $_vc[0],'|'.$_vc[1],'<br />';
//print_r($_vc);
}
}
代码如下:
foreach ($array as $key=>$value)
{
……
}
代码如下:
foreach ($array as $key=>$value)
{
echo $key.'=>'.$value;
}
本文来自:http://www.q1010.com/173/13611-0.html
注:关于PHP foreach循环用法示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。