W3docs

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.

You can use the implode() function to convert an array to a delimited string in PHP. Here's an example:

How to use the implode() function to convert an array to a delimited string in PHP?

<?php

$array = array('apple', 'banana', 'cherry', 'date');
$delimiter = ',';

$string = implode($delimiter, $array);
echo $string; // Outputs: apple,banana,cherry,date

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

You can then save this string in the database. To convert the delimited string back to an array, you can use the explode() function:

How to use the explode() function to convert the delimited string back to an array in PHP?

<?php

$string = 'apple,banana,cherry,date';
$delimiter = ',';

$array = explode($delimiter, $string);
print_r($array); // Outputs: Array ( [0] => apple [1] => banana [2] => cherry [3] => date )

Example of join() function in PHP

<?php

$array = array('apple', 'banana', 'cherry', 'date');
$delimiter = ',';

$string = join($delimiter, $array);
echo $string; // Outputs: apple,banana,cherry,date