VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • PHP foreach()跳出本次或当前循环与终止循环方法

 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
复制代码
复制代码

 

下面是自己的实际例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//父级栏目
    public function parentList($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($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;
                }else{
                    //没有子类
                    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",$praductData);
 
        }
 
        return view("ParentList");
    }