2013-10-26 37 views
1
$arr = array('not want to print','foo','bar'); 

foreach($arr as $item) { 
    switch($item) { 
     case 'foo': 
     $item = 'bar'; 
     break; 
     case 'not want to print': 
     continue; 
     break; 
    } 

    echo $item; 
} 

http://codepad.org/WvW1Fmmo使用繼續從跳過內部SWICH

但 「not want to print」 中的foreach迭代呼應。爲什麼繼續不適用於foreach?

+0

你拼錯'switch'。這是在真正的代碼或複製錯誤? – Barmar

+0

嗨,謝謝;不,它實際上是一個樣本http://codepad.org/Ytkd8x2M –

+1

'不想打印'vs'不想打印' - 錯字 – jancha

回答

5

http://php.net/manual/en/control-structures.continue.php

注意:注意在PHP中switch聲明被認爲是一個循環結構的繼續目的。

因此,使用continue 2;來繼續包含它的循環。

您還有一個$arrcase之間的不匹配。數組中值的第一個單詞是no,但是您在case中檢查not

Corrected codepad

+0

感謝您的領導,從未聽說過它。其實我的代碼更大,沒有錯別字,我試圖重現更簡單的代碼版本,哈哈。再次感謝! –