文本爆炸和循环php结果是错误的 但我尝试这个文本结果1至50行 但结果更重复
<?php
$text = "1*10 11*20 21*30 31*40 41*50";
$textAr = explode("\n", str_replace("\r", "", $text));
$textAr = array_filter($textAr, 'trim');
foreach ($textAr as $line) {
$pieces = explode("*", $line);
list($first, $last) = array_pad(explode('*', $line, 2), 2, null);
$add = 1;
for ($i = $first; $i <= $last; $i += $add) {
$array[] = array(
'count' => $i
);
}
$codes = $array;
foreach ($codes as $code) {
echo $code['count'];
echo "<br>";
}
}
?>
结果: 150行
1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,11,12,13,14 ,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,1 3,14,15,16 ,17,18,19,20,21,22,23,24,25,26,27,28,29,30,1,2,3,4,5,6,7,8,9 ,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,2 6,27,28,29,30,31, 32,33,34,35,36,37,38,39,40,1,2,3,4,5,6,7,8,9,10,11,12,13,14 15,16,17,18,19,20,21,22,23,24,25,26,27,28,2 9,30,31,32,33,34,35, 36,37,38,39,40,41,42,43,44,45,46,47,48,4 9,50
但我想要结果,但我想要结果1-50没有重复
答案 0 :(得分:1)
您只需将打印件移到foreach循环外部即可。 此代码将根据输入[即$ text]添加1到10,11到20等的数字。 并在最后打印所有这些。
<?php
$text = "1*10
11*20
21*30
31*40
41*50";
$textAr = explode("\n", str_replace("\r", "", $text));
$textAr = array_filter($textAr, 'trim');
foreach ($textAr as $line) {
$pieces = explode("*", $line);
list($first, $last) = array_pad(explode('*', $line, 2), 2, null);
$add = 1;
for ($i = $first; $i <= $last; $i += $add) {
$array[] = array(
'count' => $i
);
}
}
$codes = $array;
foreach ($codes as $code) {
echo $code['count'];
echo "<br>";
}
?>
结果:
1
2
3
4
5
6
7
8
9
10 <登记/> 11 12
13
14
15
16
17
18
19
20 <登记/> 21 22
23
24
25
26
27
28
29
30 <登记/> 31 32
33
34
35
36
37
38
39
40 <登记/> 41 42
43
44
45
46
47
48
49
50 < BR />