PHP implode() and explode()

implode()

implode() function is used to join arrays. It joins an array with a given glue and returns a string.

The implode function has two arguments. implode(glue, array)

  1. glue - the string which connects each array element
  2. array - the array to be joined

implode() with indexed arrays


<?php
$array = ['Breakfast', 'Lunch', 'Dinner'];
echo implode(', ', $array);
// Breakfast, Lunch, Dinner

implode() with associative arrays

When using implode() with associative arrays, the keys will be ignored. Values will be joined.


<?php
$array = [
    'best' => 'Apple',
    'worst' => 'Pineapple',
    'good' => 'Banana'
];
echo implode('|', $array);
// Apple|Pineapple|Banana

Note: implode() cannot be used with multi-dimensional arrays

implode() using different glues

Let's play with the glue argument in the implode() function.


<?php
$array = ['Apple', 'Banana', 'Pineapple'];

echo implode('', $array);			// AppleBananaPineapple
echo implode(' and ', $array);		// Apple and Banana and Pineapple
echo implode('<br>', $array);    # using HTML

// WTH? Reverse works? Yes!! PHP knows your mistakes
echo implode($array, ',');

explode()

explode() function is used to split strings. It splits a string by a delimeter, and returns an array.

The explode function has two arguments. explode(delimeter, string)

  1. delemeter - the string which is used to split. (The boundary)
  2. string - the string to be splitted

If the string is Apple,Banana, and the delimeter is ,, you will get an array with two elements Apple and Banana. The important thing to notice is that delimeter is not included in the splitted elements. They work excatly as boundaries.

explode() Basic Examples


<?php
$str = 'Apple, Banana, Cherry';
print_r( explode(',', $str) );  // There's an anoying whitespace

// No more whitespaces in element
// now the boundary (delimeter) is ', '
// string chunks around the delimeter will be elements of array
print_r( explode(', ', $str) ); 

explode() with list()


<?php
$str = 'Hyvor Developer:developer.hyvor.com';
list($title, $url) = explode(':', $str);
echo  $title;
echo $url;

Unlike implode(), explode()'s arguments cannot be reversed. The reason is: in implode(), each argument was in different data types (string and array). But, both arguments of explode() are strings. Therefore PHP doesn't have a way to identify them seperately. So, for both functions, remember to use the delimeter or glue first to reduce mistakes.

Tagged: PHP
Liked the article? Follow me on Twitter, or help Hyvor grow by funding Hyvor Talk.
Latest on Hyvor Developer
PHP Beginner's Tutorial
Beginner's PHP Tutorial
Image for Creating a Real-Time Chat App with PHP and Node.js
Creating a Real-Time Chat App with PHP and Node.js
Image for All About MYSQLI Prepared Statements in PHP
All About MYSQLI Prepared Statements in PHP
Image for Image Upload with AJAX, PHP, and MYSQL - The Beginner's Guide
Image Upload with AJAX, PHP, and MYSQL - The Beginner's Guide
Image for PHP Contact Form - The Email Method
PHP Contact Form - The Email Method
Image for The Best Way to Perform MYSQLI Prepared Statements in PHP
The Best Way to Perform MYSQLI Prepared Statements in PHP
Related Articles
7