php数组迭代&串联

时间:2011-03-11 20:44:25

标签: php arrays

我有一个类似下面的数组,我想检查下一个元素是否以空格开头,如果是,则将它连接到前一个元素。

[1] => Array (
    [1] => lenny/volatile/main Packages
    [2] => lenny/volatile/main Packages
    [3] => lenny/volatile/main Sources
    [4] =>  Reading package
    [5] => lenny/volatile/main Sources
)

Output:

[1] => Array (
    [1] => lenny/volatile/main Packages
    [2] => lenny/volatile/main Packages
    [3] => lenny/volatile/main Sources Reading package
    [5] => lenny/volatile/main Sources
)

谢谢!

6 个答案:

答案 0 :(得分:3)

$count = count($array);
for($i = 0; $i < $count; $i++){
    if($array[$i][0] == ' '){
        if($i > 0){
             $array[$i-1] .= $array[$i];
             unset($array[$i])
        }
    }
}

应该这样做(即如果您的数组名为$array,则另外符合您的需要)

答案 1 :(得分:1)

一种方法来检查PHP demo

//your starting array
$myarray = array("lenny/volatile/main Packages","lenny/volatile/main Packages", "lenny/volatile/main Sources", " Reading package", "lenny/volatile/main Sources");

$mystring = implode(",", $myarray);  //implode array into a string delimited by ,
echo $mystring.PHP_EOL.PHP_EOL;  //debug

$mystring = str_replace(", ", ' ', $mystring); //str replace all ", " with ' '
echo $mystring.PHP_EOL.PHP_EOL; //debug

$result= explode(',',$mystring);  //explode back into an array with delimiter ','
print_r($result);  //should give you final result

输出结果@Michael:

lenny/volatile/main Packages,lenny/volatile/main Packages,lenny/volatile/main Sources, Reading package,lenny/volatile/main Sources

lenny/volatile/main Packages,lenny/volatile/main Packages,lenny/volatile/main Sources Reading package,lenny/volatile/main Sources

    Array
    (
        [0] => lenny/volatile/main Packages
        [1] => lenny/volatile/main Packages
        [2] => lenny/volatile/main Sources Reading package
        [3] => lenny/volatile/main Sources
    )

答案 2 :(得分:1)

@manitor你忘记在删除之前将值添加到上一行。

应该是这样的:

for($i = 0; $i < count($array); $i++){
    if($array[$i][0] == ' '){
        $array[$i-1].= $array[$i];
        unset($array[$i]);
    }
}

答案 3 :(得分:1)

这会按照您希望的方式创建一个新数组:

$target = array(array_shift($array));
$to = 0;
foreach($array as $string) {
    if($string[0] === ' ') {
        $target[$to] .= $string;
    }
    else {
        $target[] = $string;
        $to++;
    }
}

DEMO

答案 4 :(得分:0)

这是完全未经测试的代码,但它可能会让您走上正轨。我会遍历数组中的每一个。如果很酷,请将其添加到新阵列。如果没有连接到前一个节点。我添加了一个指针,以便您的索引将保持打包在新数组中。

//$my_array already full of stuff
$new_array = array();
$pointer = 0;
for($i = 0; $i < count($my_array); $i++) {
    if($i == 0) {
        $new_array[$pointer] = $my_array[$i];
    } else {
        $string = $my_array[$i];
        if(substr($string, 0, strlen($string) - 1) == ' ') {
            $new_array[$pointer - 1] .= $string;
        } else {
            $new_array[$pointer] = $string;
        }
    }
    $pointer++;
}

答案 5 :(得分:0)

在这里尝试异国情调只是为了好玩;)尝试使用最少量的线条来使其发挥作用。

阵列:

$myAr = Array(
    0 => "lenny/volatile/main Packages",
    1 => "lenny/volatile/main Packages",
    2 => "lenny/volatile/main Sources",
    3 => " Reading package",
    4 => "lenny/volatile/main Sources",
);

功能调用&amp;逻辑(将丢弃字符串,因为没有前一项)

array_walk($myAr, 'concat', &$myAr);

function concat($item, $key, $ar) {
 if(preg_match("/^(\s+)(.*)/", $item, $matches) == 1) {
   unset($ar[$key]);
   if (isset($ar[--$key])){
     $ar[$key] .= $matches[0];
   }
 }
}

结果:

var_dump($myAr);

array(4) { 
  [0]=> string(28) "lenny/volatile/main Packages" 
  [1]=> string(28) "lenny/volatile/main Packages" 
  [2]=> string(43) "lenny/volatile/main Sources Reading package" 
  [4]=> string(27) "lenny/volatile/main Sources" 
}