How to create a string by joining the values of an array in PHP

Topic: PHP / MySQL« Prev|Next »

Answer: Use the PHP implode() or join() function

You can use the PHP implode() or join() function for creating a string by joining the values or elements of an array with a glue string like comma (,) or hyphen (-).

  • <?php
  • $array = array("red", "green", "blue");
  •  
  • // Turning array into a string
  • $str = implode(", ", $array);
  • echo $str; // Outputs: red, green, blue
  • ?>

Related FAQ

Here are some more FAQ related to this topic:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.