在php中数组分为一维数组与多维数组,如果我们要循环二维数组我们可以直接使用foreach与for来遍历即可,下面我来给大家介绍几个二维数组循环的实现有需要了解的朋友可进入参考。
for循环遍历二维数组
代码如下 |
复制代码 |
代码如下 |
复制代码 |
print_r($order_data_info);
Array ( [0] => stdClass Object ( [countOrder] => 3 [amntTotal] => 2380.00 [name] => 测试版 ) [1] => stdClass Object ( [countOrder] => 1 [amntTotal] => 2380.00 [name] => VIP版 ) [2] => stdClass Object ( [countOrder] => 2 [amntTotal] => 4760.00 [name] => 个人版 ) )
$order_length = sizeof($order_data_info); for($i=0;$i<$order_length; $i) { $order_data_info[$i]->amntTotal = money($order_data_info[$i]->amntTotal); }
print_r($order_data_info);
Array ( [0] => Array ( [countOrder] => 3 [amntTotal] => 2380.00 [name] => 测试版 ) [1] => Array ( [countOrder] => 1 [amntTotal] => 2380.00 [name] => VIP版 ) [2] => Array ( [countOrder] => 2 [amntTotal] => 4760.00 [name] => 个人版 ) )
$order_length = sizeof($order_data_info); for($i=0;$i<$order_length; $i) { $order_data_info[$i]['amntTotal'] = money($order_data_info[$i]['amntTotal']); } |
print_r($order_data_info); Array ([0] => stdClass Object ( [countOrder] => 3 [amntTotal] => 2380.00 [name] => 测试版 ) [1] => stdClass Object ( [countOrder] => 1 [amntTotal] => 2380.00 [name] => VIP版 )
代码如下 |
复制代码 |
foreach($array as $key) { if(xxxx) { break; //break 会终止循环 } if(zzz) { continue; //continue 会终止本次循环,此刻会进入下一个循环; } } |
[2] => stdClass Object ( [countOrder] => 2 [amntTotal] => 4760.00 [name] => 个人版 ))
代码如下 |
复制代码 |
$team = array('lk','ok'); $book = array('linux服务器配置与管理',$team); foreach($book as $k=>$val) //意思是for $book each $value( as ) if( is_array($val) ) foreach( $val as $value) echo $value.' '; else echo $k.'=>'.$val.' '; ?> |
$order_length = sizeof($order_data_info);for($i=0;$i<$order_length; $i)<🎜>{<🎜> $order_data_info[$i]->amntTotal = money($order_data_info[$i]->amntTotal);}print_r($order_data_info);Array([0] => Array ( [countOrder] => 3 [amntTotal] => 2380.00 [name] => 测试版 )[1] => Array ( [countOrder] => 1 [amntTotal] => 2380.00 [name] => VIP版 )[2] => Array ( [countOrder] => 2 [amntTotal] => 4760.00 [name] => 个人版 ))$order_length = sizeof($order_data_info);for($i=0;$i<$order_length; $i)<🎜>{<🎜> $order_data_info[$i]['amntTotal'] = money($order_data_info[$i]['amntTotal']);<🎜>}<🎜> |
<🎜><🎜>foreach语句用于循环遍历数组。<🎜><🎜>每进行一次循环,当前数组元素的值就会被赋值给 value 变量(数组指针会逐一地移动) - 以此类推<🎜>
代码如下 |
复制代码 |
<🎜>foreach($array as $key)<🎜>{<🎜> if(xxxx)<🎜> {<🎜> break; //break 会终止循环<🎜> }<🎜> if(zzz)<🎜> {<🎜> continue; //continue 会终止本次循环,此刻会进入下一个循环;<🎜> }<🎜>}<🎜> |
<🎜>例<🎜>
代码如下 |
复制代码 |
<🎜> $team = array('lk','ok');<🎜> $book = array('linux服务器配置与管理',$team);<🎜> <🎜> foreach($book as $k=>$val) //意思是for $book each $value( as ) if( is_array($val) ) foreach( $val as $value) echo $value.' '; else echo $k.'=>'.$val.' ';?> |
例2
代码如下
代码如下 |
复制代码 |
$array = array('塞班(symbian)'=>array(1=>'s60v3横版',2=>'s60v3',3=>'s60v5'), '安卓(android)'=>array(4=>'android2.1及以上版本',6=>'Android 1.5',7=>'Android 1.6',8=>'Android 2.1',9=>'Android 2.2',10=>'Android 2.3',11=>'Android 3.0',12=>'Android 3.1',12=>'Android 3.2',12=>'Android 4.0',12=>'Android 4.1',12=>'Android 4.2'), 'wp7(Windowsphone)'=>array(5=>'Windows Phone 7系统') );
foreach( $array as $c=> $cc ) { echo ''; foreach($cc as $v => $vv) { if( $addRow["sjos"]== $v ) { $select="selected";}else{ $select="";} echo ''; } } ?>
|
|
复制代码
|
|
$array = array('塞班(symbian)'=>array(1=>'s60v3横版',2=>'s60v3',3=>'s60v5'),
'安卓(android)'=>array(4=>'android2.1及以上版本',6=>'Android 1.5',7=>'Android 1.6',8=>'Android 2.1',9=>'Android 2.2',10=>'Android 2.3',11=>'Android 3.0',12=>'Android 3.1',12=>'Android 3.2',12=>'Android 4.0',12=>'Android 4.1',12=>'Android 4.2'), 'wp7(Windowsphone)'=>array(5=>'Windows Phone 7系统')
);
foreach( $array as $c=> $cc )
{ echo '
'; foreach($cc as $v => $vv) { if( $addRow["sjos"]== $v ) { $select="selected";}else{ $select="";} echo '
'; } }?>
教程地址:欢迎转载!但请带上文章地址^^