0

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!

0

6 Answers 6

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

He doesn't want items that start with a space to be eliminated; he wants them appended to the previous item.
Note that count is being run on every iteration. Might be worth storing that in a variable somewhere, or even using a foreach, since you're using both the key and value, anyway. *shrug*
1

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

Comments

1

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
    )

6 Comments

Cute :) Raw iteration is probably significantly faster, but I like the simplicity.
@matchu i like less lines ;D thanks. made it cleaner w/ comment
I might be looking wrong but the explode would do nothing right, you just removed all , by a single space. I guess you want to replace ", " with ", " (double for single space)
@Michael the explode just pretty much make it back into an array w/ delimiter set as , so basically you concatinate the $mystring elements and then explode it back to an array and out put it like what the poster want as the final result (an array).
@kjy seems to work but I'm missing something here, aren't your removing all , in the string with the str_replace?
|
1

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.