In PHP, the implode
function is used to join elements of an array into a single string. It takes two arguments: a string that separates the array elements in the resulting string, and an array to join.
For example:
$array = ['apple', 'banana', 'orange'];
$string = implode(', ', $array);
// $string is now 'apple, banana, orange'
The explode
function is the opposite of implode
. It takes a string and a delimiter as arguments and returns an array of strings created by splitting the input string at each occurrence of the delimiter.
For example:
$string = 'apple, banana, orange';
$array = explode(', ', $string);
// $array is now ['apple', 'banana', 'orange']
You can also specify a limit argument to the explode
function to control the maximum number of array elements that should be returned. For example:
$string = 'apple, banana, orange, lemon, lime';
$array = explode(', ', $string, 3);
// $array is now ['apple', 'banana', 'orange, lemon, lime']
The explode
function is often used to parse simple delimiter-separated strings, such as CSV (comma-separated values) or TSV (tab-separated values) files.