Jump to content

ghelp getting all pieces of an exploded array


Jnerocorp

Recommended Posts

Hello,

 

its supposed to take the $output and change it

 

original = john,18,Cancer

After    = 'john', '18', 'Cancer'

 

but right now it does this:

 

original = john,18,Cancer

After    = 'Cancer'

 

this is my code so far:

 

<?php

$output = "john,18,Cancer";

echo "Original Output: $output <br>";

$explode = explode(",", $output);

$wordChunks = explode(",", $output);
for($i = 0; $i < count($wordChunks); $i++){
$newoutput = "'$wordChunks[$i]', ";
}

echo "New Output: " . $newoutput . "<br>";

echo "<br>";

$removecomma = substr_replace($newoutput, "", -2);

echo "Remove Final Comma: $removecomma";

echo "<br>";

?>

 

thanks for any help

 

-John

change:

 

for($i = 0; $i < count($wordChunks); $i++){
   $newoutput = "'$wordChunks[$i]', ";
}

 

to:

 

for($i = 0; $i < count($wordChunks); $i++){
   $newoutput .= "'$wordChunks[$i]', ";
}

 

notice the .= to concatenate the string.  right now, you just keep resetting the variable $newoutput which is why you're only getting one value in the output.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.