满足条件时,如何在 PHP 中中断 for 循环?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1053964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:47:59  来源:igfitidea点击:

How do I break a for-loop in PHP when conditions are met?

phploopsprimes

提问by Alex Mcp

I'm diligently plugging away at some code that checks for divisibility (yes, it's to generate primes) and I want to know how to stop a for... loop if the condition is met once. Code like this:

我正在努力编写一些检查可分性的代码(是的,它是为了生成素数),我想知道如果条件满足一次,如何停止 for... 循环。像这样的代码:

$delete = array();
foreach ( $testarray as $v ) {
    for ( $b = 2; $b < $v; $b++ ) {
        if ( $v % $b == 0 ) {
            $delete []= $v;
        }
    }

So $testarrayis integers 1-100, and the $deletearray will be filtered against the $testarray. Currently though, a number like 12 is being added to $deletemultiple times because it's divisible by 2, 3, 4, and 6. How can I save my computer's time by skipping ahead when the criteria matched once?

所以$testarray是整数1-100,和$delete数组将针对被过滤$testarray。但目前,像 12 这样的数字被$delete多次添加,因为它可以被 2、3、4 和 6 整除。当条件匹配一次时,我如何通过跳过来节省计算机的时间?