在foreach循环中合并变量

时间:2017-07-18 10:10:26

标签: php algorithm

我试图在foreach循环中合并变量。

这是我的代码:

// example string for $arr[3] = (1 - 9, 11 - 12, 16 - 17, 20 - 22, 24 - 25, 34 - 37, 39 - 42);

$nice = $arr[3];                     
$arr2 = explode(",", $nice);
foreach ($arr2 as $val1) {
    if (strpos($val1, ' - ') != FALSE) {
        $val1 = trim($val1);
        $str1 = substr($val1, 0, 2);
        $str2 = substr($val1, -2);                          
        $nice = range($str1, $str2);                        
        print_r($nice); 

        foreach ($nice as $nice_neu) {                      
            $nice_fin .= ", " . $nice_neu;                                                        
        }
    }
}  

但是我得到了这个结果!

nice neu:, 35, 36, 41, 42, 35, 36, 41, 42, 41, 42, 41, 42, 35, 36, 38, 39, 35, 36, 41, 42, 1

nice neu:, 35, 36, 41, 42, 35, 36, 41, 42, 41, 42, 41, 42, 35, 36, 38, 39, 35, 36, 41, 42, 1, 2

依旧......

结果应如下所示:

1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 16, 17, 20, 21, 22, 24, 25, 34, 35, 36, 37, 39, 40, 41, 42

谢谢!

0 个答案:

没有答案