PHP foreach () or out of the current cycle and the current cycle termination method

PHPforeach () or out of the current cycle and the current cycle termination method

 PHP using foreach () loop, you want to at the time of the cycle, when certain conditions are met, want

Copy the code
$ ARR = Array ( 'A', 'B', 'C', 'D', 'E'); 
$ HTML = ''; 
the foreach ($ ARR AS $ Key => $ value) { 
    IF ($ value = = 'B') { 
        $ HTML = $ value;. 
        Continue; // when $ value is b, out of this cycle 
    } 
    iF ($ value == 'C') { 
        . $ HTML = $ value; 
        BREAK; / / when $ value is c, the loop is terminated 
    } 
    $ HTML = $ value;. 
} 
echo $ HTML; // output: ab
Copy the code

 

Here is an example of their actual

// Parent column 
    public function parentList ($ ID) 
    { 
        // query the data in all subclasses column 
        $ categoryData = db ( "category" ) -> where ( "id =" $ id.) -> find (); 
        the dump // ($ CategoryData); 
        IF ($ CategoryData!) { 
            $ this-> error ( "parameter error"); 
        } the else { 
            $ this-> ASSIGN ( 'CategoryData', $ CategoryData); 
            // query the lower section All subclasses data i.e. $ ID = PID 
            $ DB = category ( "category") -> SELECT (); 

            $ praProductData = $ this-> toLayer ($ category); 
       // the dump ($ praProductData); 
            the foreach ($ praProductData AS K = $> $ V) { 
                // if subcategory 
                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 = $ Praductdt-> render ();
            }

            $this->assign('Page',$page);
            $this->assign("newPraProduct",$praductData);

        }

        return view("ParentList");
    }  

Print $ praProductData results are as follows:

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) "Beverage supermarket air curtain cabinet " 
        [" seo_keys "] => String (21 is)" Beverage supermarket air curtain cabinet "
        [" seo_title "] => String (0)" "
        [" Status "] => int (. 1)
        [ "seo_desc"] => string (21) " Beverage supermarket air curtain cabinet" 
        [" zilei "] => array (0
        } 
      } 
      [. 1] => Array (. 9) { 
        [ "ID"] => int (37 [) 
        [ "PID"] => int ( . 1) 
        [ "path"] => String (. 6) "0-1-37" 
        [ "name"] => String (15) "supermarket air curtain cabinet" 
        [ "Status"] => int (. 1) 
        [ " seo_title "] => String (0)" " 
        [" seo_keys "] => String (15)" supermarket air curtain cabinet " 
        [" seo_desc "] => String (15)" supermarket air curtain cabinet " 
        [" 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) "鲜花柜"
    ["status"] => 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) {
        }
      }
    }
  }
}

  

To get out of this cycle continues the next cycle, or when certain conditions are met, the termination foreach () loop, respectively, will be used: continue and break.

Guess you like

Origin www.cnblogs.com/zmdComeOn/p/11126011.html