3

I remember there was an easy way without a for each to reconstruct an array into a string.

EG:

1 > my
2 > name
3 > is
4 > sam

into $string = "my name is sam" (with spaces in between).

How was this done with php?

3 Answers 3

8

You just need to implode the string:

$array = array("my", "name", "is", "sam");
$string = implode(' ', $array);
echo $string; // "my name is sam"
Sign up to request clarification or add additional context in comments.

Comments

3

Try this:

<?php

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

print $comma_separated; // lastname,email,phone

?>

1 Comment

Or to print one by line: $one_per_line = implode(PHP_EOL, $array);
2

You should use the implode method. You can read more about it on the following link: http://php.net/manual/en/function.implode.php.

Your code would be something like:

$string = implode(" ", $array);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.