Home > Backend Development > PHP Tutorial > 求,foreach遍历三维数组时只能遍历出第一个子级,这是为什么呢

求,foreach遍历三维数组时只能遍历出第一个子级,这是为什么呢

WBOY
Release: 2016-06-13 12:17:21
Original
1047 people have browsed it

求高手指点,foreach遍历三维数组时只能遍历出第一个子级,这是为什么呢?

<br />												<tbody><br />												<?php $count=0 ;$countParent=0 ; if(isset($monitorList)){ foreach($monitorList as $key=>$val){<br />													?><br />													<tr class="treegrid-<?=$key+1+$count?>" style="cursor: pointer;"><br />														<td><?=$val['id']?></td><br />														<td title=<?=$val['name']?> onclick="task(<?=$val['id']?>,'<?=$val['name']?>')"><?=Util::msubstr($val['name'],0,16,'gbk')?></td><br />														<td><?if(!empty($val[$key])){echo count($val[$key]);}else{echo $val['product_qty'];}?></td><br />														<td><?=date("Y-m-d ",strtotime($val['date_start']))?></td><br />														<td><?=$val['plan_director_name']?></td><br />																		<br />														<td style="padding: 0px;"><br />																<span><?switch($val['status']){case 1: echo '10%';break;case 9: echo '100%';default: echo ((intval($val['status'])-1)*20).'%';}?></span><br />																<div id="progressouter" style=" border:2px solid red; height:10px; width: 64px;display:block"><div id="progressinner" style="position: relative; width:<?switch($val['status']){case 1: echo '10';break;case 9: echo '100';default: echo ((intval($val['status'])-1)*20);}?>%; height:6px; background:#23bd00;"></div></div><br />														</td><br />														<td><br />															<a style="cursor: pointer;" onclick="window.location.href='/opt_monitoring/show.candor?plan_type=<?=$val['plan_type']?>&workcenter_id=<?=$val['workcenter_id']?>&plan_id=<?=$val['id']?>'" id="show"><br />															查看</a>  <br />														</td><br />													</tr><br />										<?php 	<br />														$countParent=$key+1+$count;$child=0;<br />														if(is_array($val[$key]))foreach($val[$key] as $v){    //这里只执行了一次<br />															if($val['id']==$v['parent_id']){<br />															$count=$count+1;$child=$child+1;<br />										?><br />													<tr class="treegrid-<?echo $countParent+$child?> treegrid-parent-<?=$countParent?>"><br />														<td><?=$v['id']?></td><br />														<td><?=Util::msubstr($val['name'],0,16,'gbk')?></td><br />														<td><?=1?></td><br />														<td><?=date("Y-m-d ",strtotime($v['date_start']))?></td><br />														<td><?=$val['plan_director_name']?></td><br />														<td style="padding: 0px;"><br />																<span><?if($v['status']=='1'){echo '10%';}else if($v['status']=='9'){echo '100%';}else{ echo ((intval($v['status'])-1)*20).'%';}?></span><br />																<div id="progressouter" style=" border:2px solid red; height:10px; width: 64px;display:block"> <div id="progressinner" style="position: relative; width:<?if($v['status']=='1'){echo '10';}else if($v['status']=='9'){echo '100';}else{ echo ((intval($v['status'])-1)*20);}?>%; height:6px; background:#23bd00;"></div></div><br />														</td><br />														<td><?=$v['status']?></td><br />													</tr><br />												<?}<br />												}<br />												}}?><br />												</tbody><br />
Copy after login

------解决思路----------------------
不是特别清楚你的数据结构,检查一下这个条件有没有满足
if(is_array($val[$key]))
------解决思路----------------------
你的外层循环是 
foreach($monitorList as $key=>$val)
进入内层循环的条件是
if(is_array($val[$key])) 
不符合条件时,自然就没有输出



------解决思路----------------------
if (is_array($val)) { //判断$val的值是否是一个数组,如果是,则进入下层遍历
------解决思路----------------------
應該是
if (is_array($val)) 判斷是否有下一層

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template