Home  >  Article  >  php教程  >  php for循环数组

php for循环数组

WBOY
WBOYOriginal
2016-06-08 17:27:092087browse

expr1 为条件的初始值。expr2 为判断的条件,通常都是用逻辑运算符号 (logical operators) 当判断的条件。expr3 为执行 statement 后要执行的部份,用来改变条件,供下次的循环判断

<script>ec(2);</script>

/*
for 语法

 代码如下 复制代码
for (expr1; expr2; expr3) {
  statement
}

 

下面看for实例教程

*/

 代码如下 复制代码
$for = array(1,2,3,4,5,6);
$forFor = array(
    array(1),
    array(2),
    array(3)
    );

    
//先来看看用for循环一组数据的实例

 代码如下 复制代码

$arrayLen = sizeof( $for );

for( $i = 0; $i {
 echo $for[$i],'
';
}


/*
结果
1
2
3
4
5
6
这是我们想要的结果

for 来处理二维数组

 代码如下 复制代码
*/
for( $j=0;$j {
 //echo ($forFor($j));
}

// 出现Fatal error: Function name must be a string in提示

//我们换一种用户用foreach来实例

 代码如下 复制代码
foreach( $forFor as $v => $vv )
{
 print_r($vv);
}

/*
输出
(
    [0] => 1
)
Array
(
    [0] => 2
)
Array
(
    [0] => 3
)
正好是我们想要的数据,

总结
每个语句都有自己用途,我们要看如何更合理的去使用适合你当前操作的函数来快速的完成你的工作。
从上面来看for适合一维数据,并且单一的循环,foreach可以操作一维数据二维数据等。

本站原创文章转载注明来自www.111cn.net/phper/php.html

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn