php中两个 foreach 嵌套,第一层的值不循环 怎么办?
$category=pdo_get($this->tablename,array('username'=>$username,'status'=>'1'));$table...
$category = pdo_get($this->tablename,array('username'=>$username,'status'=>'1'));
$tablegrade = pdo_getall($this->tableclass,array('pid'=>$category['id'],'type' => 1));
foreach ($tablegrade as $v){
$tableclass = pdo_getall($this->tableclass,array('pid'=>$v['id'],'type'=>'2'));
foreach($tableclass as $value){
$gcdetail[] = array(
'cid' => $value['id'],
'gcname' => $v['name'].$value['name']
);
}
}
var_dump($gcdetail);
输出结果为:
array(3) {
[0]=>
array(2) {
["cid"]=>
string(2) "55"
["gcname"]=>
string(22) "一年级一(1)班"
}
[1]=>
array(2) {
["cid"]=>
string(2) "56"
["gcname"]=>
string(22) "一年级一(2)班"
}
[2]=>
array(2) {
["cid"]=>
string(2) "57"
["gcname"]=>
string(22) "一年级一(3)班"
}
}
理论上还有二三四五六年级,但是第一层foreach没有继续进行.,,求大家帮忙解决. 展开
$tablegrade = pdo_getall($this->tableclass,array('pid'=>$category['id'],'type' => 1));
foreach ($tablegrade as $v){
$tableclass = pdo_getall($this->tableclass,array('pid'=>$v['id'],'type'=>'2'));
foreach($tableclass as $value){
$gcdetail[] = array(
'cid' => $value['id'],
'gcname' => $v['name'].$value['name']
);
}
}
var_dump($gcdetail);
输出结果为:
array(3) {
[0]=>
array(2) {
["cid"]=>
string(2) "55"
["gcname"]=>
string(22) "一年级一(1)班"
}
[1]=>
array(2) {
["cid"]=>
string(2) "56"
["gcname"]=>
string(22) "一年级一(2)班"
}
[2]=>
array(2) {
["cid"]=>
string(2) "57"
["gcname"]=>
string(22) "一年级一(3)班"
}
}
理论上还有二三四五六年级,但是第一层foreach没有继续进行.,,求大家帮忙解决. 展开
1个回答
展开全部
代码本身没看到啥问题,估计是没数据,你改成这样试试:
$category = pdo_get($this->tablename, array('username' => $username, 'status' => '1'));
$tablegrade = pdo_getall($this->tableclass, array('pid' => $category['id'], 'type' => 1));
foreach ($tablegrade as $v) {
$tableclass = pdo_getall($this->tableclass, array('pid' => $v['id'], 'type' => '2'));
var_dump($tableclass);// 如果只打印了一年级的数据,说明你确实没数据
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询