分解并循环php结果的文本错误
分解并循环php结果的文本错误 但我试着把这段文字改成1到50行 但结果更加重复分解并循环php结果的文本错误,php,arrays,explode,Php,Arrays,Explode,分解并循环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)
<?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,13,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,26,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,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50
但是我想要结果,但是我想要结果1-50,没有重复的您只需要将打印移到foreach循环之外。 此代码将数字从1添加到10,从11添加到20。。根据输入[即$text]执行etc。 最后打印所有的
<?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
1<
1<
2
2<
2
2<
1
1
1<
1
1<
1<
2
2
2
3
3
3
3
3<
3<<
3<
3
3
3
3
3<<<<<<<<<<<<<<<<<<
3
3
3<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3
3
3
3
3
3<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
49
50
示例:请缩进您的代码,它现在很难阅读。顺便说一句,给出一个结果示例,并解释为什么它是错误的。结果是1501,6 77 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 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但是我想输入这个文本的结果1-50,这看起来不错,因为您使用了
$array[]
,它将把从$first
到$last
的所有$i
添加到数组中。顺便说一句,你应该编辑你的文章,并在里面添加这些例子[我得到了,我想要,因为那样]只要复制链接的结果并粘贴到这里是的,我错了,在foreach循环之外工作很好谢谢