这篇文章主要介绍PHP中如何使用foreach()跳出本次或当前循环与终止循环,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
PHPforeach()跳出本次或当前循环与终止循环方法
PHP中用foreach()循环中,想要在循环的时候,当满足某个条件时,想
复制代码
$arr=array(‘a’,’b’,’c’,’d’,’e’);
$html=”;
foreach($arras$key=>$value){
if($value==’b’){
$html.=$value;
continue;//当$value为b时,跳出本次循环
}
if($value==’c’){
$html.=$value;
break;//当$value为c时,终止循环
}
$html.=$value;
}
echo$html;//输出:ab
复制代码
下面是自己的实际例子
//父级栏目
publicfunctionparentList($id)
{
//查询该栏目下所有子类数据
$categoryData=db(“category”)->where(“id=”.$id)->find();
//dump($categoryData);
if(!$categoryData){
$this->error(“参数错误”);
}else{
$this->assign(‘categoryData’,$categoryData);
//查询该栏目下所有子类数据即pid=$id
$category=db(“category”)->select();
$praProductData=$this->toLayer($category);
//dump($praProductData);
foreach($praProductDataas$k=>$v){
//如果有子类目
if(!empty($v[‘zilei’])&&$v[‘id’]==$id){
$praductData=Db::table(“ven_article”)
->alias(‘a’)
->join(‘ven_categoryw’,’a.cid=w.id’)
->where(“a.status=1andw.pid='”.$id.”‘”)
->field(“a.id,a.title,a.faceimg,a.time_report,a.resume,a.cid,w.name”)
->order(‘a.idDESC’)
->paginate(6);
//dump($praductData);
break;
}else{
//没有子类
if($v[‘id’]==$id){
$praductData=Db::table(“ven_article”)
->alias(‘a’)
->join(‘ven_categoryw’,’a.cid=w.id’)
->where(“a.status=1anda.cid=”.$id)
->field(“a.id,a.title,a.faceimg,a.time_report,a.resume,a.cid,w.name”)
->order(‘a.idDESC’)
->paginate(6);
//dump($praductData);
break;
}
}
}
$data=$praductData->toArray()[‘data’];//或者$data=$praductDatas->all();
if($data){
$this->assign(‘a’,1);
}
$page=$praductData->render();
$this->assign(‘Page’,$page);
$this->assign(“newPraProduct”,$praductData);
}
returnview(“ParentList”);
}
打印$praProductData结果如下:
array(5){
[0]=>array(9){
[“id”]=>int(1)
[“pid”]=>int(0)
[“path”]=>string(3)”0-1″
[“name”]=>string(9)”风幕柜”
[“status”]=>int(1)
[“seo_title”]=>string(0)””
[“seo_keys”]=>string(0)””
[“seo_desc”]=>string(0)””
[“zilei”]=>array(2){
[0]=>array(9){
[“id”]=>int(2)
[“pid”]=>int(1)
[“path”]=>string(5)”0-1-2″
[“name”]=>string(21)”饮料超市风幕柜”
[“status”]=>int(1)
[“seo_title”]=>string(0)””
[“seo_keys”]=>string(21)”饮料超市风幕柜”
[“seo_desc”]=>string(21)”饮料超市风幕柜”
[“zilei”]=>array(0){
}
}
[1]=>array(9){
[“id”]=>int(37)
[“pid”]=>int(1)
[“path”]=>string(6)”0-1-37″
[“name”]=>string(15)”超市风幕柜”
[“status”]=>int(1)
[“seo_title”]=>string(0)””
[“seo_keys”]=>string(15)”超市风幕柜”
[“seo_desc”]=>string(15)”超市风幕柜”
[“zilei”]=>array(0){
}
}
}
}
[1]=>array(9){
[“id”]=>int(33)
[“pid”]=>int(0)
[“path”]=>string(4)”0-33″
[“name”]=>string(15)”环岛风幕柜”
[“status”]=>int(1)
[“seo_title”]=>string(15)”环岛风幕柜”
[“seo_keys”]=>string(15)”环岛风幕柜”
[“seo_desc”]=>string(15)”环岛风幕柜”
[“zilei”]=>array(0){
}
}
[2]=>array(9){
[“id”]=>int(46)
[“pid”]=>int(0)
[“path”]=>string(4)”0-46″
[“name”]=>string(9)”点菜柜”
[“status”]=>int(1)
[“seo_title”]=>string(9)”点菜柜”
[“seo_keys”]=>string(9)”点菜柜”
[“seo_desc”]=>string(9)”点菜柜”
[“zilei”]=>array(0){
}
}
[3]=>array(9){
[“id”]=>int(48)
[“pid”]=>int(0)
[“path”]=>string(4)”0-48″
[“name”]=>string(9)”鲜花柜”
[“sta免费云主机域名tus”]=>int(1)
[“seo_title”]=>string(9)”鲜花柜”
[“seo_keys”]=>string(9)”鲜花柜”
[“seo_desc”]=>string(9)”鲜花柜”
[“zilei”]=>array(0){
}
}
[4]=>array(9){
[“id”]=>int(53)
[“pid”]=>int(0)
[“path”]=>string(4)”0-53″
[“name”]=>string(9)”药品柜”
[“status”]=>int(1)
[“seo_title”]=>string(0)””
[“seo_keys”]=>string(0)””
[“seo_desc”]=>string(0)””
[“zilei”]=>array(2){
[0]=>array(9){
[“id”]=>int(54)
[“pid”]=>int(53)
[“path”]=>string(7)”0-53-54″
[“name”]=>string(15)”药品阴凉柜”
[“status”]=>int(1)
[“seo_title”]=>string(0)””
[“seo_keys”]=>string(0)””
[“seo_desc”]=>string(0)””
[“zilei”]=>array(0){
}
}
[1]=>array(9){
[“id”]=>int(55)
[“pid”]=>int(53)
[“path”]=>string(7)”0-53-55″
[“name”]=>string(15)”药品冷藏柜”
[“status”]=>int(1)
[“seo_title”]=>string(0)””
[“seo_keys”]=>string(0)””
[“seo_desc”]=>string(0)””
[“zilei”]=>array(0){
}
}
}
}
}
要跳出本次循环继续执行下次循环,或者满足某个条件的时候,终止foreach()循环,分别会用到:continue与break。以上是“PHP中如何使用foreach()跳出本次或当前循环与终止循环”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注百云主机行业资讯频道!
本文小编为大家详细介绍“Java虚拟机中内存区域怎么分配”,内容详细,步骤清晰,细节处理妥当,希望这篇“Java虚拟机中内存区域怎么分配”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。程序计数器(Program Counter Reg…
免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。