Suppose that you have an PHP array of fruits, and you want to convert it into a comma-separated list like this:

PHP

This article will guide you on how to achieve that: Convert from array to string in PHP.

There are plenty of methods to do so, which we will provide all of them, from traditional for, foreach to using implode functions, and we will give our opinions on the advantages and disadvantages of each method.

Why so many methods? Isn’t implode already good?

We always try to be creative and flexible in PHP development, so it will pay off in case of large-scale projects which doesn’t always good to write code using one method.

3 methods to convert array to string in PHP

We will use the given fruits array as mentioned in the intro:

PHP

1. Using for() loop

This is the traditional method to convert array to string using PHP. We will:

  • Loop through the array elements
  • Extract the text from each element and push it to the $output
  • Add the comma (,) if it is not the last element (to prevent obsolete comma at the end)

PHP

We will get the result as expected:

PLAINTEXT

*Quick tips: The conditional check for last element can be used with the array_key_last function, which will return the last element’s key in the array. However, this function only work with PHP 7.3.0+. Example:

PHP

See PHP.net Official Documentation on array_key_last.

Pros of using for loop

  • The iteration is clearly defined (by the $i variable)
  • Break (stop) conditional is easier to specify

Cons of using foreach loop

  • Worse performance than foreach loop
  • Cannot be used with string-based element keys
  • count() function has to be used properly

2. Using foreach() loop

Another array looping method to convert array to string using PHP is foreach loop. It’s mostly similar with for loop, just a little different with the syntax:

PHP

Again, this will provide us with the similar result.

Pros of using foreach loop

  • Better performance than for loop
  • Can be used with string-based element keys

Cons of using foreach loop

  • The iteration is not clearly defined (had to declare $key => $value)
  • Break (stop) condition has to be clearly defined

3. Using implode() function

Finally, the simplest method to convert array to string in PHP is the implode function. It will simply join the elements in the array into a text string, using the first parameter as the separator.

Syntax: implode(string $separator, array $array)

In our example, this is how we use implode to achieve the result:

PHP

This will instantly show the result as expected.

Pros of using implode function

  • Easy to implement
  • Can avoid the last obsolete separator compared to looping methods

Cons of using implode function

  • Looping can work better with element conditional check

What to use?

If you simply want to join the array elements into one string, use implode.

If not only joining but extra conditional check is required, like excluding elements by specific criteria, using for and foreach is recommended.

Final thoughts

That’s all for this how-to guide on the methods to convert from array to string using PHP. Hopefully this guide could help you with your PHP development. We always constantly update and release new helpful PHP tutorials.

Thanks for reading!