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
You can connect with me on Twitter or Linkedin.
Latest on My Blog
PHP Beginner's Tutorial
Beginner's PHP Tutorial
Image for Laravel High CPU Usage Because of File-based Session Storage
Laravel High CPU Usage Because of File-based Session Storage
Image for Resizing Droplets: A Personal Experience
Resizing Droplets: A Personal Experience
Image for Moving our CDN (10+ GB images) to a new server
Moving our CDN (10+ GB images) to a new server
Image for Disqus, the dark commenting system
Disqus, the dark commenting system
Image for Creating a Real-Time Chat App with PHP and Node.js
Creating a Real-Time Chat App with PHP and Node.js
Related Articles
1224