text to explode and loop php result is wrong but i try to this text to result 1 to 50 line but result is more duplicate
<?php
$text = "1*10 11*20 21*30 31*40 41*50";
$textAr = explode("
", str_replace("", "", $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>";
}
}
?>
Results : 150 lines
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
but I want results but i want result 1-50 with no duplicates
You just need to move the print outside of the foreach loop. This code will add the number from 1 to 10, 11 to 20 .. etc according to the input [which is $text]. And print all of them at the end.
<?php
$text = "1*10
11*20
21*30
31*40
41*50";
$textAr = explode("
", str_replace("", "", $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>";
}
?>
Result :
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
Example : http://ideone.com/Tk052M