您好,登錄后才能下訂單哦!
這篇文章主要介紹PHP中如何使用foreach()跳出本次或當(dāng)前循環(huán)與終止循環(huán),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
PHPforeach()跳出本次或當(dāng)前循環(huán)與終止循環(huán)方法
PHP中用foreach()循環(huán)中,想要在循環(huán)的時(shí)候,當(dāng)滿足某個(gè)條件時(shí),想
復(fù)制代碼
$arr=array('a','b','c','d','e');
$html='';
foreach($arras$key=>$value){
if($value=='b'){
$html.=$value;
continue;//當(dāng)$value為b時(shí),跳出本次循環(huán)
}
if($value=='c'){
$html.=$value;
break;//當(dāng)$value為c時(shí),終止循環(huán)
}
$html.=$value;
}
echo$html;//輸出:ab
復(fù)制代碼
下面是自己的實(shí)際例子
//父級(jí)欄目
publicfunctionparentList($id)
{
//查詢?cè)摍谀肯滤凶宇悢?shù)據(jù)
$categoryData=db("category")->where("id=".$id)->find();
//dump($categoryData);
if(!$categoryData){
$this->error("參數(shù)錯(cuò)誤");
}else{
$this->assign('categoryData',$categoryData);
//查詢?cè)摍谀肯滤凶宇悢?shù)據(jù)即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{
//沒(méi)有子類
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結(jié)果如下:
array(5){
[0]=>array(9){
["id"]=>int(1)
["pid"]=>int(0)
["path"]=>string(3)"0-1"
["name"]=>string(9)"風(fēng)幕柜"
["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)"飲料超市風(fēng)幕柜"
["status"]=>int(1)
["seo_title"]=>string(0)""
["seo_keys"]=>string(21)"飲料超市風(fēng)幕柜"
["seo_desc"]=>string(21)"飲料超市風(fēng)幕柜"
["zilei"]=>array(0){
}
}
[1]=>array(9){
["id"]=>int(37)
["pid"]=>int(1)
["path"]=>string(6)"0-1-37"
["name"]=>string(15)"超市風(fēng)幕柜"
["status"]=>int(1)
["seo_title"]=>string(0)""
["seo_keys"]=>string(15)"超市風(fēng)幕柜"
["seo_desc"]=>string(15)"超市風(fēng)幕柜"
["zilei"]=>array(0){
}
}
}
}
[1]=>array(9){
["id"]=>int(33)
["pid"]=>int(0)
["path"]=>string(4)"0-33"
["name"]=>string(15)"環(huán)島風(fēng)幕柜"
["status"]=>int(1)
["seo_title"]=>string(15)"環(huán)島風(fēng)幕柜"
["seo_keys"]=>string(15)"環(huán)島風(fēng)幕柜"
["seo_desc"]=>string(15)"環(huán)島風(fēng)幕柜"
["zilei"]=>array(0){
}
}
[2]=>array(9){
["id"]=>int(46)
["pid"]=>int(0)
["path"]=>string(4)"0-46"
["name"]=>string(9)"點(diǎn)菜柜"
["status"]=>int(1)
["seo_title"]=>string(9)"點(diǎn)菜柜"
["seo_keys"]=>string(9)"點(diǎn)菜柜"
["seo_desc"]=>string(9)"點(diǎn)菜柜"
["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){
}
}
}
}
}
要跳出本次循環(huán)繼續(xù)執(zhí)行下次循環(huán),或者滿足某個(gè)條件的時(shí)候,終止foreach()循環(huán),分別會(huì)用到:continue與break。
以上是“PHP中如何使用foreach()跳出本次或當(dāng)前循環(huán)與終止循環(huán)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。