Home » Programming » PHP » Php Implode

Using the PHP Array ‘implode’ Function, with Examples

We’ve previously covered the explode() function in PHP – now it’s time for implode()!

This tutorial will teach you how to merge values from an array into a single string using the PHP implode() function.

Processing data for presentation to the end-user is one of the common PHP tasks.

PHP is frequently used on Linux to display database query results to the user. If some of those values are stored in an array, you may want to merge them into a single string for ease of display or brevity – this is what the implode() function does.

Syntax

implode ( $glue , $pieces )

Note that:

  • $glue is the character or characters, you wish to insert between the $pieces when merging them
    • It can be an empty string if you want no separator
  • $pieces is an array containing the strings you wish to combine into a single string
  • The function returns a merge string containing the $pieces from the array, with the $glue character(s) inserted between them

Example

In this example, we receive an array of address elements which we want to turn into a single string for display:

$address_pieces = [
    "Apartment 3", 
    "Swanston Street", 
    "Melbourne", 
    "Australia"
];
$merged_address = implode(",", $address_pieces);
echo $merged_address; //Returns  "Apartment 3, Swanston Street, Melbourne, Australia"
  • $address_pieces is an array – in this case, a list of separate string values
  • The PHP code defining $address_pieces has been spread over several lines for readability
  • See our other PHP array articles for more on working with PHP arrays
  • echo is used to output the result of *implode() to the browser
  • $merged_address is now a single string, with all of the pieces glued together with a “,” (standard comma) separating them

Using newlines as glue

We can also glue the pieces together using newline characters so that the final $merged_address displays to the user over several lines rather than a single comma-separated line:

$address_pieces = [
    "Apartment 3", 
    "Swanston Street", 
    "Melbourne", 
    "Australia"
];
$merged_address = implode("\r\n", $address_pieces);
echo $merged_address; 
// Returns:
// "Apartment 3
// Swanston Street
// Melbourne
// Australia"
  • Instead of a comma, we’ve used “\r\n” as our glue
  • “\r\n” represents a new-line character in PHP strings and will cause the text following it to start on the next line
    • It is composed of two parts:
      • \r represents a carriage return and is required for Windows PCs
      • \n represents the new line on UNIX systems such as Linux
      • You can usually get away with using just \n – both are used here in sequence to make sure that the newline displays regardless of the platform in use

Conclusion

The PHP implode() function sounds less exciting than the explode() function, but is just as useful.

Click here for more PHP tutorials from LinuxScrew!

To view the official documentation for the PHP implode() function:

https://www.php.net/manual/en/function.implode.php

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment