explode() and implode() php arrays

No this isn’t about getting frustrated with PHP and nuking your server, this is a couple of very useful (although hard to remember) array functions.

explode() will take a string and give you an array, splitting that string on the delimiter you give. For example, if you have a list of comma seperated values you will work this as follows:

{code type=php}
$monkeys = ‘bubbles,charlie,george’;
$monkeysArray = explode($monkeys,’,’);
var_dump($monkeysArray); # shows you the array

There is also an optional third parameter, $limit, which will restrict the number of array elements created.

implode() will do the opposite; pass it an array and the glue and in return you will get a string of each array element joined by the glue:

{code type=php}
$monkeysArray = array(‘bubbles’,’charlie’,’george’);
$monkeys = implode(‘ and ‘,$monkeysArray);
echo $monkeys; # gives you “bubbles and charlie and george”

I could never remember which function is which, so I now imagine “exploding” a string to create an array.


Leave a Reply

Your email address will not be published.

More Posts

How to use PHP Type Declarations

PHP has developed into a more strictly typed language in recent years. Type Declarations help with that and are a fairly new addition to PHP,


If you have come to JavaScript from a background in Java or C, then you will like the variation on the for loop. It is

Context Object Manipulation

One of the key concepts of JavaScript is the keyword this, which signifies the JavaScript context object. If you write an inner function, you can access

Polymorphic Functions

It is possible to define multiple functions with the same name in JavaScript, and the last one of those that you define will be the