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

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

face issues in loop and break in php

问题

以下是您要的翻译部分:

  1. 我在我的简单代码中遇到了无法解决的问题
  2. foreach(file('num.txt') as $num) {
  3. foreach(file('char.txt') as $char) {
  4. echo $char.$num. "<br>";
  5. if($char=="C"&&$num==3){
  6. echo 'Found it<br>';
  7. break;
  8. }
  9. }
  10. echo '----------------------------<br>';
  11. }
  12. num.txt 包含如下数字
  13. 1
  14. 2
  15. 3
  16. 4
  17. 5 每行一个
  18. char 包含字符 A B C D 每行一个
  19. .
  20. 结果是
  21. break 后的文本中没有删除 C3
  22. 但我想要的是这种方式,不包括找到的 C3
  23. A1
  24. B1
  25. C1
  26. D1
  27. --------------
  28. A2
  29. B2
  30. C2
  31. D2
  32. --------------
  33. A3
  34. B3
  35. C3
  36. Found it
  37. D3
  38. --------------
  39. A4
  40. B4
  41. D4
  42. --------------
  43. A5
  44. B5
  45. D5
  46. --------------
  47. 所以 C 不再在循环中,并且如果添加 B5 等,继续下去
  48. 请帮忙,我需要完成这个任务

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

英文:

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

  1. foreach(file(&#39;num.txt&#39;) as $num) {
  2. foreach(file(&#39;char.txt&#39;) as $char) {
  3. echo $char.$num. &quot;&lt;br&gt;&quot;;
  4. if($char==&quot;C&quot;&amp;&amp;$num==3){
  5. echo &#39;Found it&lt;br&gt;&#39;;
  6. break;
  7. }
  8. }
  9. echo &#39;----------------------------&lt;br&gt;&#39;;
  10. }

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

  1. A1
  2. B1
  3. C1
  4. D1
  5. --------------
  6. A2
  7. B2
  8. C2
  9. D2
  10. --------------
  11. A3
  12. B3
  13. C3
  14. Found it
  15. D3
  16. --------------
  17. A4
  18. B4
  19. D4
  20. --------------
  21. A5
  22. B5
  23. D5
  24. --------------

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",但由于您从文件中隐式获取数据,这是解决方案。不是最优雅的方法,但可以工作:

  1. // 添加一个标志以表示找到的值
  2. $found = false;
  3. foreach(file('num.txt') as $num) {
  4. foreach(file('char.txt') as $char) {
  5. // 条件来显示一个条目
  6. if(!$found || $char != "C") {
  7. echo $char . $num . "<br>";
  8. if($char == "C" && $num == 3) {
  9. echo '找到了<br>';
  10. // 您不需要 'break',删除它并设置标志
  11. $found = true;
  12. }
  13. }
  14. }
  15. echo '----------------------------<br>';
  16. }

希望这可以帮助您。

英文:

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:

  1. // add flag for found value
  2. $found = false;
  3. foreach(file(&#39;num.txt&#39;) as $num) {
  4. foreach(file(&#39;char.txt&#39;) as $char) {
  5. // condition to show an entry
  6. if(!$found || $char != &quot;C&quot;) {
  7. echo $char.$num. &quot;&lt;br&gt;&quot;;
  8. if($char == &quot;C&quot; &amp;&amp; $num == 3) {
  9. echo &#39;Found it&lt;br&gt;&#39;;
  10. // you do not need &#39;break&#39;, remove it and set flag instead
  11. $found = true;
  12. }
  13. }
  14. }
  15. echo &#39;----------------------------&lt;br&gt;&#39;;
  16. }

答案2

得分: 0

I think your code will be like this:

  1. foreach(file('num.txt') as $num) {
  2. foreach(file('char.txt') as $char) {
  3. if($char == "C" && $num == 3) {
  4. echo 'Found it<br>';
  5. break;
  6. } else {
  7. echo $char . $num . "<br>";
  8. }
  9. }
  10. echo '----------------------------<br>';
  11. }
英文:

I think your code will be like this:

  1. foreach(file(&#39;num.txt&#39;) as $num) {
  2. foreach(file(&#39;char.txt&#39;) as $char) {
  3. if($char==&quot;C&quot;&amp;&amp;$num==3){
  4. echo &#39;Found it&lt;br&gt;&#39;;
  5. break;
  6. }
  7. else{
  8. echo $char.$num. &quot;&lt;br&gt;&quot;;
  9. }
  10. }
  11. echo &#39;----------------------------&lt;br&gt;&#39;;
  12. }

答案3

得分: 0

希望这有所帮助。

  1. <?php
  2. $arr1 = ['A','B','C','D'];
  3. $arr2 = [1,2,3,4,5];
  4. for($i = 0; $i < count($arr1); $i++) {
  5. for($j = 0; $j < count($arr2); $j++){
  6. if($arr1[$i] == "C" && $arr2[$j] == 3){
  7. echo "找到了<br/>";
  8. break;
  9. }
  10. echo $arr1[$i]."".$arr2[$j]."<br/>";
  11. }
  12. echo "----------------------<br/>";
  13. }
  14. ?>

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

英文:

Hope this help.

  1. &lt;?php
  2. $arr1 = [&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;];
  3. $arr2 = [1,2,3,4,5];
  4. for($i = 0; $i &lt; count($arr1); $i++) {
  5. for($j = 0; $j &lt; count($arr2); $j++){
  6. if($arr1[$i] == &quot;C&quot; &amp;&amp; $arr2[$j] == 3){
  7. echo &quot;Found It&lt;br/&gt;&quot;;
  8. break;
  9. }
  10. echo $arr1[$i].&quot;&quot;.$arr2[$j].&quot;&lt;br/&gt;&quot;;
  11. }
  12. echo &quot;----------------------&lt;br/&gt;&quot;;
  13. }
  14. ?&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:

确定