简体   繁体   中英

php array iteration & concatenate

I have an array that looks like the one below, I would like to check if the next elem starts with a space, if it does, concatenate it to the previous elem.

[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
)

Thanks!

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

That should do it (that is if your array is named $array , otherwise suit it to your needs)

one way to do it checkout the 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

output 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
    )

@manitor you forgot to add the value to the previous line bfore deleting.

should read like that:

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

This creates a new array the way you want it:

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

DEMO

This is completely untested code but it might get you on the right track. I would iterate over each one in the array. If it is cool add it to the new array. If not concat it to the previous node. I added a pointer so that your index will remain packed in the new array.

//$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++;
}

Tried something exotic here just for fun ;) tried to use a minimal amount of lines to get it working.

Array:

$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",
);

Function call & logic (will drop the string is there is no previous item)

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];
   }
 }
}

Result:

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" 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM