英文:
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('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 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('num.txt') as $num) {
foreach(file('char.txt') as $char) {
// condition to show an entry
if(!$found || $char != "C") {
echo $char.$num. "<br>";
if($char == "C" && $num == 3) {
echo 'Found it<br>';
// you do not need 'break', remove it and set flag instead
$found = true;
}
}
}
echo '----------------------------<br>';
}
答案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('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>';
}
答案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/>";
}
?>
英文:
Hope this help.
<?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 "Found It<br/>";
break;
}
echo $arr1[$i]."".$arr2[$j]."<br/>";
}
echo "----------------------<br/>";
}
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论