PHP "Array to String" (Examples of How to Use the implode Function)

Writer: PJ Fancher

PJ Fancher, Contributing Expert

PJ is a professional web developer with more than 20 years of experience. His background began with writing small web applications using Perl, PHP, and MySQL in 1998. He has well-rounded experience with all aspects of web development, from server and database configuration to scripting, styling, design, and SEO.

Editor: Lillian Castro

Lillian Castro, Senior Editor

Lillian Castro brings more than 30 years of editing and journalism experience to our team. She has written and edited for major news organizations, including The Atlanta Journal-Constitution and the New York Times, and she previously served as an adjunct instructor at the University of Florida. Today, she edits HostingAdvice content for clarity, accuracy, and reader engagement.

Reviewer: Cristian Lopez

Cristian Lopez, News Manager

Cristian Lopez uses his Business Marketing background from the University of Illinois at Chicago to create comfortable environments for customers, clients, and colleagues to share their thoughts and ideas openly. From interviewing tech leaders to conducting UX market research projects, Cristian knows the importance of storytelling — a key variable for innovation and inspiration. His goal at HostingAdvice is to wow readers on the ever-evolving nature of the tech industry and bring his audience the most reliable and exciting content on all things hosting.

Disclosure: When you purchase through links on our site, we often earn referral fees from partners. For more information, see our ad disclosure and review policy.

Follow Us:
2.7k
16k
5.7k
134
3.5k

In PHP, the implode() method joins array elements and outputs them as a single string. This is useful when you need to create one string out of a set of array values.

The function can be invoked in two ways (with or without the glue string):

  1. implode( string $glue , array $pieces ) – The glue is used to combine the array pieces.
  2. implode( array $pieces ) – No glue is used, so the pieces will be concatenated together.

We’ll go through examples of both, as well as how to go from a string to an array, below.

Example: Using implode to Go From an Array to a String

To output an array as a string, where each array value is separated by a comma and a space, you would do the following:

$list = array( 'foo', 'bar', 'baz' );  echo implode( ", ", $list );   // outputs: foo, bar, baz  

Without the previous glue string comma, this would be the result:

$list = array( 'foo', 'bar', 'baz' );  echo implode( $list );    // outputs: foobarbaz  

It’s Worth Knowing About print_r, var_dump, and json_encode

Other functions that prove useful for looking at the values of arrays are print_r and var_dump. These two functions can be extremely useful when you are debugging a program and want to see the output of an array (or any variable).

Also, json_encode will take your array data and output it as a string in JSON-encoded format.

Bonus: How To Go From a String to an Array (The Reverse of Implode)

The reverse action — going from a string to an array — can be done easily with explode, or by using the preg_split function, which takes a regular expression.

Using explode

You guessed it: explode is the opposite of implode and uses a delimiter to decide where to break up a string. It is usually the simplest way to break up a string into an array.

$keywords = explode( ', ', 'foo, bar, baz' );  print_r( $keywords );

The above would result in the output below:

(      [0] => foo      [1] => bar      [2] => baz  )

Using preg_split

If your delimiter is REALLY complicated, it might make sense to use a regular expression to define it. If this is the case, you can use preg_split.

$keywords = preg_split( "/[,]+/", 'foo,bar,baz' );  print_r( $keywords );

This would also output the following:

(      [0] => foo      [1] => bar      [2] => baz  )

Okay, that is enough PHP string manipulation for now. Just don’t be afraid to implode!

Advertiser Disclosure

HostingAdvice.com is a free online resource that offers valuable content and comparison services to users. To keep this resource 100% free, we receive compensation from many of the offers listed on the site. Along with key review factors, this compensation may impact how and where products appear across the site (including, for example, the order in which they appear). HostingAdvice.com does not include the entire universe of available offers. Editorial opinions expressed on the site are strictly our own and are not provided, endorsed, or approved by advertisers.

Our Editorial Review Policy

Our site is committed to publishing independent, accurate content guided by strict editorial guidelines. Before articles and reviews are published on our site, they undergo a thorough review process performed by a team of independent editors and subject-matter experts to ensure the content’s accuracy, timeliness, and impartiality. Our editorial team is separate and independent of our site’s advertisers, and the opinions they express on our site are their own. To read more about our team members and their editorial backgrounds, please visit our site’s About page.

About the Author

PJ Fancher
Contributing Expert

PJ Fancher is a professional web developer with more than 20 years of experience. His background began with writing small web applications using Perl, PHP, and MySQL in 1998. He has well-rounded experience with all aspects of web development, from server and database configuration to scripting, styling, design, and SEO. Today he brings his passion for web development to the team at Digital Brands Inc., helping to develop unique, high-quality, highly-optimized websites. He is ready to share his extensive knowledge and experience with the world of web hosting.

« BACK TO: HOW-TO

Meet the Experts

Our team of experts with a combined 50+ years of experience in web hosting serve insight and advice to more than 20 million users!

We Know Hosting

$

4

8

,

2

8

3

spent annually on web hosting!