1

私は次のようなphpforeachステートメントを使用しています:

<?php foreach($files as $f): ?>

たくさんのHTML

<?php endforeach; ?>

次の反復にスキップするように、ループ内に条件を配置するにはどうすればよいですか。私はcontinueを使用することになっていることを知っていますが、このような閉じたphpステートメントでそれを行う方法がわかりません。それは別のphpステートメントでしょうか?ループ内のすべてではないが一部が実行されるように、HTMLの途中に配置できますか?

4

2 に答える 2

3

はい、条件をcontinue好きな場所に挿入できます。

<?php foreach($files as $f): ?>

lots of HTML

<?php if (condition) continue; ?>

more HTML

<?php endforeach; ?>

実際の動作をご覧ください

于 2012-08-30T22:42:41.677 に答える
0

私は非常によく似た問題を抱えていて、すべての検索が私をここに導きました。誰かが私の投稿がお役に立てば幸いです。私自身のコードから:PHP 5.3.0の場合、これは機能しました:

foreach ($aMainArr as $aCurrentEntry) {
    $nextElm = current($aMainArr); //the 'current' element is already one element ahead of the already fetched but this happens just one time!
    if ($nextElm) {
        $nextRef = $nextElm['the_appropriate_key'];
        next($aMainArr); //then you MUST continue using next, otherwise you stick!
    } else { //caters for the last element
    further code here...
    }
//further code here which processes $aMainArr one entry at a time...
}

PHP 7.0.19の場合、以下が機能しました。

reset($aMainArr);
foreach ($aMainArr as $aCurrentEntry) {
    $nextElm = next($aMainArr);
    if ($nextElm) {
        $nextRef = $nextElm['the_appropriate_key'];
    } else { //caters for the last element
    further code here...
    }
//further code here which processes $aMainArr one entry at a time...
}
于 2018-05-10T08:52:27.487 に答える