2011-11-09 153 views
-1

之後我怎麼能剎兩次迭代後foreach循環,這樣的事情:打破foreach循環兩次迭代

foreach($posttags as $tags){  
    $tag1 = $tags->slug; 
} --- this is the first iteration, stored in the variable $tag1 

foreach($posttags as $tags){  
    $tag2 = $tags->slug; 
} ---- this is the second iteration, stored in the variable $tag2 

brake; --- brake the foreach loop 

任何想法?

+0

難道你不能同時設置兩個? –

+2

中斷可能不會制動代碼,但制動器肯定不會按預期中斷。 – 2011-11-09 02:09:59

+0

*'foreach'之後的'break' *?它應該打破什麼?另外,在一個循環中,兩個*循環*不同於兩個*迭代*。 – deceze

回答

2

最明顯的答案是不使用foreach循環,如果你不想循環通過每個項目。使用其他循環結構,或者手動抓取第一個和第二個項目,如果這是你真正想要的。