Convert flat array to a delimited string to be saved in the database
You can use the implode()
function to convert an array to a delimited string in PHP. Here's an example:
<?php
$array = array('apple', 'banana', 'cherry', 'date');
$delimiter = ',';
$string = implode($delimiter, $array);
echo $string; // Outputs: apple,banana,cherry,date
Watch a video course
Learn object oriented PHP
You can then save this string in the database. To convert the delimited string back to an array, you can use the explode()
function:
<?php
$string = 'apple,banana,cherry,date';
$delimiter = ',';
$array = explode($delimiter, $string);
print_r($array); // Outputs: Array ( [0] => apple [1] => banana [2] => cherry [3] => date )
<?php
$array = array('apple', 'banana', 'cherry', 'date');
$delimiter = ',';
$string = join($delimiter, $array);
echo $string; // Outputs: apple,banana,cherry,date