首页 > 作文

PHP foreach()跳出本次或当前循环与终止循环方法

更新时间:2023-04-07 06:50:29 阅读: 评论:0

phpforeach()跳出本次或当前循环与终止循环方法

php中用foreach()循环中,想要在循环的时候,当满足某个条件时,想

$arr = array('a','b','c','d','e');$html = '';foreach($arr as $key => $value){    if($value=='b'){        $html .= $value;        continue; // 当 $value为b时,跳出本次循环    }    if($value=='c'){        $html .= $value;        break; // 当 $value为c时,终止循环    }    $html .= $value;}echo $html; // 输出: ab

下面是自己的实际例子

//父级栏目    public function parentlist($id)    {        //查询该栏目下所有子类数据        $categorydata = db("category")->where("id=".$id)->find();        //dump($categorydata);        if(!$categorydata){            $this->error("参数错误");        }el{            $this->assign('categorydata',$categorydata);            //查询该栏目下所有子类数据即 pid=$id            $category = db("category")->lect();            $praproductdata = $this->tolayer($category);       //dump($praproductdata);            foreach($praproductdata as $k=>$v) {                //如果有子类目                if (!empty($v['zilei']) && $v['id']==$id) {                    $praductdata = db::table("ven_article")                        ->alias('a')                        ->join('ven_category w','a.cid=w.id')                        ->where("a.status=1 and w.pid='".$id."'")                        ->field("a.id,a.title,a.faceimg,a.time_report,a.resume,a.cid,w.name")                        ->order('a.id desc')                        ->paginate(6);                        //dump($praductdata);                        break;                }el{                    //没有子类                    if ($v['id'] == $id) {                        $praductdata = db::table("ven_article")    兰蔻面膜                            ->alias('a')                                ->join('ven_category w','a.cid=w.id')                                ->where("a.status=1 and a.cid=".$id)                                ->field("a.id,a.title,a.faceimg,a.time_report,a.resume,a.cid,w.name")                                ->order('a.id desc')                                ->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",$p熊佳琪raductdata);        }        return view("parentlist");    }  

打印$praproductdata结果如下:

array(5) {  [0] => array(9) {    ["id"] => int(1)    ["pid"] => int(0)    ["path"] => string(3) "0-1"    ["name"] => string(9) "风幕柜"  学而不厌诲人不倦  ["status"] => int(1)    ["o_title"] => string(0) ""    ["o_keys"] => string(0) ""    ["o_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)        ["o_title"] => string(0) ""        ["o_keys"] => string(21) "饮料超市风幕柜"        ["o_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)        ["o_title"] => string(0) ""        ["o_keys"] => string(15) "超市风幕柜"        ["o_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)    ["o_title"] => string(15) "环岛风幕柜"    ["o_keys"] => string(15) 什么东西比乌鸦更讨厌 "环岛风幕柜"    ["o_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)    ["o_title"] => string(9) "点菜柜"    ["o_keys"] => string(9) "点菜柜"    ["o_desc"] => string(9) "点菜柜"    ["zilei"] => array(0) {    }  }  [3] => array(9) {    ["id"] => int(48)    ["pid"] => int(0)    ["path"] => string(4) "0-48"    ["name"] => string(9) "鲜花柜"    ["status"] => int(1)    ["o_title"] => string(9) "鲜花柜"    ["o_keys"] => string(9) "鲜花柜"    ["o_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)    ["o_title"] => string(0) ""    ["o_keys"] => string(0) ""    ["o_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)        ["o_title"] => string(0) ""        ["o_keys"] => string(0) ""        ["o_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)        ["o_title"] => string(0) ""        ["o_keys"] => string(0) ""        ["o_desc"] => string(0) ""        ["zilei"] => array(0) {        }      }    }  }}

  

要跳出本次循环继续执行下次循环,或者满足某个条件的时候,终止foreach()循环,分别会用到:continue 与 break。

本文发布于:2023-04-07 06:50:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/3cc9bd28dc4f81d0e86aaedcdcc89d06.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP foreach()跳出本次或当前循环与终止循环方法.doc

本文 PDF 下载地址:PHP foreach()跳出本次或当前循环与终止循环方法.pdf

标签:环岛   超市   子类   鲜花
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图