PHP implode

Summary: in this tutorial, you’ll learn how to use the PHP implode() function to join a list of string using a separator.

Introduction to the PHP implode() function

The PHP implode() function allows you to join an array of strings by a separator. Here’s the syntax of the implode() function:

implode ( string $separator , array $array ) : string
Code language: PHP (php)

The implode() function has two parameters:

  • The $separator is the separator that separates between two strings. The $separator defaults to an empty string.
  • The $array is an array of strings to join.

The implode() function returns a new string resulting from joining string elements in the $array with the separator.

The order of string elements in the result string is the same as the order they appear on the $array.

If the array is empty, then the implode() function returns an empty string.

Note that the join() function is the alias of the implode() function so that you can use them interchangeably.

PHP implode() function examples

Let’s take some examples of using the implode() function.

1) Using the implode() function to join strings example

Suppose that you have a list of column names, including first_name, last_name, and email. And you want to turn it into a header of a CSV file.

To do that, you can use the implode() function to join the string elements like this:

<?php $columns = ['first_name', 'last_name', 'email']; $header = implode(',', $columns); echo $header;
Code language: PHP (php)

Output:

first_name,last_name,email
Code language: PHP (php)

2) Using the implode() function with the array_map() function

If you want to replace the underscore (_) in each element, you can do it as follows:

<?php $columns = ['first_name', 'last_name', 'email']; $header = implode(',', array_map(fn ($c) => str_replace('_', ' ', $c), $columns)); echo $header;
Code language: PHP (php)

How it works.

  • First, the array_map() returns a new array with each element’s underscore string (_) replaced by the space.
  • Second, the implode() joins the strings of the returned array of the array_map() function.

3) Using the implode() function with the array_filter() function

The following example illustrates how to use the implode() function with the array_filter() function:

<?php $columns = ['ssn', 'first_name', 'last_name', 'password', 'email', 'phone']; $excludes = ['ssn', 'password']; $header = implode(',', array_filter($columns, fn ($c) => !in_array($c, $excludes))); echo $header;
Code language: PHP (php)

Output:

first_name,last_name,email,phone
Code language: PHP (php)

How it works.

  • First, define two arrays of strings, one for columns and the other for excluding columns.
  • Second, use the array_filter() function to filter the columns in the excluded list.
  • Third, use the implode() function to join the strings in the array returned by the array_filter() function.

4) Using the PHP implode() function with an associative array example

If you pass an associative array to the implode() function, it will join the array’s values only and disregard the array keys. For example:

<?php $person = [ 'first_name' => 'John', 'last_name' => 'Doe', 25, 'Female' ]; echo implode(',', $person);
Code language: PHP (php)

Output:

John,Doe,25,Female
Code language: PHP (php)

5) Using the PHP implode() function with an array that has one element

The following defines a function called get_header() that returns a comma-separated list of columns:

<?php function get_header(array $columns): string { if (1 === count($columns)) { return $columns[0]; } return implode(',', $columns); }
Code language: PHP (php)

The get_header() function returns the first element in the $columns array if the $columns array has one element. Otherwise, it returns a comma-separated list of strings in the $columns array.

The following shows how to use the get_header() function:

<?php echo get_header(['first_name']); // first_name echo get_header(['first_name', 'last_name']); // first_name,last_name
Code language: PHP (php)

However, the code in the get_header() is unnecessary since the implode() function already has a logic to handle the array with one element. For example:

<?php echo implode(',', ['first_name']); // first_name echo implode(',', ['first_name', 'last_name']); // first_name,last_name
Code language: PHP (php)

When the input array has one element, the implode() function doesn’t add a trailing separator, which is the comma in this example.

Summary

  • Use the PHP implode() function to join strings in an array with a separator between each element.
Did you find this tutorial useful?