php二维数组循环程序代码

作者:袖梨 2022-11-14

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']);
}


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 '';
}

}
?>

相关文章

精彩推荐