Coding, PHP & MySQL

Convert an array into a delimited string in PHP

Using the implode() method to convert your array into a delimited string, then use explode() to reverse the process and convert the concatenated string back into an array.

Use the implode() method to convert your array into a delimited string, then use explode() to reverse the process and convert the concatenated string back into an array.

Use the implode and explode functions available in PHP. implode takes an array, and will return a string with all the items in a list. Explode takes a list of items in a string, and gives you back an array. You specify the character(s) that are used as the dividers. For example:

$test = Array('one','two','five','seven');
foreach($test as $value) echo $value.'<br />';
$tstring = implode(',' , $test);
echo $tstring.'<br />';
$test2 = explode(',' $tstring);
foreach($test2 as $value) echo $value.'<br />';

You Might Also Like