要分解和循环的文本 php 结果是错误的

text to explode and loop php result is wrong

要分解和循环的文本 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 没有重复

您只需将打印移出 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

示例:http://ideone.com/Tk052M