在PHP中循环和break时出现问题。

huangapple go评论80阅读模式
英文:

face issues in loop and break in php

问题

以下是您要的翻译部分:

我在我的简单代码中遇到了无法解决的问题

foreach(file('num.txt') as $num) {
    foreach(file('char.txt') as $char) {

        echo $char.$num. "<br>";
        if($char=="C"&&$num==3){
            echo 'Found it<br>';
            break;
        }
    }
    echo '----------------------------<br>';

}
num.txt 包含如下数字
1
2
3
4
5 每行一个
char 包含字符 A B C D 每行一个
. 
结果是
在 break 后的文本中没有删除 C3

但我想要的是这种方式,不包括找到的 C3

    A1
    B1
    C1
    D1
    --------------
    A2
    B2
    C2
    D2
    --------------
    A3
    B3
    C3
    Found it
    D3
    --------------
    A4
    B4
    D4
    --------------
    A5
    B5
    D5
    --------------

所以 C 不再在循环中,并且如果添加 B5 等,继续下去
请帮忙,我需要完成这个任务

希望这有助于您理解代码问题并解决它。

英文:

I've faced problem that I cant resolve in my simple code

foreach(file(&#39;num.txt&#39;) as $num) {
	foreach(file(&#39;char.txt&#39;) as $char) {

	    echo $char.$num. &quot;&lt;br&gt;&quot;;
        if($char==&quot;C&quot;&amp;&amp;$num==3){
	        echo &#39;Found it&lt;br&gt;&#39;;
	        break;
        }
	}
	echo &#39;----------------------------&lt;br&gt;&#39;;
   
}

num.txt contains numbers like this
1
2
3
4
5 per line
char contains character Like A B C D per line
.
and as result
it dose not remove the C3 in text step after break

but i want it like this way without the founded C3

A1
B1
C1
D1
--------------
A2
B2
C2
D2
--------------
A3
B3
C3
Found it
D3
--------------
A4
B4
D4
--------------
A5
B5
D5
--------------

so C is no longer in the loop and continue if add B5 and so on
Please help i need this assignment

答案1

得分: 0

如果您有一个常规的数组,您可以从中移除 "C",但由于您从文件中隐式获取数据,这是解决方案。不是最优雅的方法,但可以工作:

// 添加一个标志以表示找到的值
$found = false;

foreach(file('num.txt') as $num) {
    foreach(file('char.txt') as $char) {

        // 条件来显示一个条目
        if(!$found || $char != "C") {
            echo $char . $num . "<br>";
            if($char == "C" && $num == 3) {
                echo '找到了<br>';
                // 您不需要 'break',删除它并设置标志
                $found = true;
            }
        }
    }
    echo '----------------------------<br>';

}

希望这可以帮助您。

英文:

If you had regular array, you could remove "C" from it, but since you are getting data implicitly from file, this is the solution. Not the most elegant, but works:

// add flag for found value
$found = false;

foreach(file(&#39;num.txt&#39;) as $num) {
    foreach(file(&#39;char.txt&#39;) as $char) {

        // condition to show an entry
        if(!$found || $char != &quot;C&quot;) {
            echo $char.$num. &quot;&lt;br&gt;&quot;;
            if($char == &quot;C&quot; &amp;&amp; $num == 3) {
                echo &#39;Found it&lt;br&gt;&#39;;
                // you do not need &#39;break&#39;, remove it and set flag instead
                $found = true;
            }
        }
    }
    echo &#39;----------------------------&lt;br&gt;&#39;;

}

答案2

得分: 0

I think your code will be like this:

foreach(file('num.txt') as $num) {
    foreach(file('char.txt') as $char) {
        if($char == "C" && $num == 3) {
            echo 'Found it<br>';
            break;
        } else {
            echo $char . $num . "<br>";
        }
    }
    echo '----------------------------<br>';
}
英文:

I think your code will be like this:

foreach(file(&#39;num.txt&#39;) as $num) {
        foreach(file(&#39;char.txt&#39;) as $char) {
             if($char==&quot;C&quot;&amp;&amp;$num==3){
                echo &#39;Found it&lt;br&gt;&#39;;
                break;
            }
            else{
            echo $char.$num. &quot;&lt;br&gt;&quot;;
           }
           
        }
        echo &#39;----------------------------&lt;br&gt;&#39;;
    
    }

答案3

得分: 0

希望这有所帮助。

<?php
    $arr1 = ['A','B','C','D'];
    $arr2 = [1,2,3,4,5];

    for($i = 0; $i < count($arr1); $i++) {
      for($j = 0; $j < count($arr2); $j++){
        if($arr1[$i] == "C" && $arr2[$j] == 3){
          echo "找到了<br/>";
          break;
        }
        echo $arr1[$i]."".$arr2[$j]."<br/>";
      }
      echo "----------------------<br/>";
    }
?>

在PHP中循环和break时出现问题。

英文:

Hope this help.

&lt;?php
    $arr1 = [&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;];
    $arr2 = [1,2,3,4,5];

    for($i = 0; $i &lt; count($arr1); $i++) {
      for($j = 0; $j &lt; count($arr2); $j++){
        if($arr1[$i] == &quot;C&quot; &amp;&amp; $arr2[$j] == 3){
          echo &quot;Found It&lt;br/&gt;&quot;;
          break;
        }
        echo $arr1[$i].&quot;&quot;.$arr2[$j].&quot;&lt;br/&gt;&quot;;
      }
      echo &quot;----------------------&lt;br/&gt;&quot;;
    }
?&gt;

在PHP中循环和break时出现问题。

huangapple
  • 本文由 发表于 2020年1月6日 15:33:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定