Srivathsan
Srivathsan

Reputation: 630

Questions Related To Implode Function PHP

I am exploding an array appending it and then I am imploding the array back. Everything works fine but I got a small doubt. Please take the example below.

$x = "123,456,789"
explode (' ', $x);
$x[] = "987";
implode (',', $x);

The output looks like the following :

,123,456,789,987

The problem is that the comma appears before the values. I want them to appear after just like the following

123,456,789,987,

Upvotes: 0

Views: 93

Answers (2)

Igor Parra
Igor Parra

Reputation: 10348

$x = '123,456,789'; // use ' instead " (just for performance)
$x = explode (',', $x);
$x[] = '987';
$y = implode (',', $x);

echo $y . ','; // add trailing comma (what nickb said)

// output: 
123,456,789,987,

About the single vs double quotes issue please refers to PHP strings and Is there a performance benefit single quote vs double quote in php?

Upvotes: 0

John Conde
John Conde

Reputation: 219824

You're exploding on a blank space instead of a comma:

explode (' ', $x);

should be

explode (',', $x);

Upvotes: 3

Related Questions