How to Split a Comma Delimited/separated String into an Array in PHP?

Problem:

You have a string consisting of some comma separated sub-strings (see the following). With those sub-strings, you want to form an array.

<?php
$languages = “Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday”;
?>

Solution:

You can use explode() function to split the above string separating by comma and create an array. See the following example-

<pre>
<?php
$languages = “Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday”;
$lan_arr = explode(‘, ’, $languages);
print_r($lan_arr);
?>
</pre>

Output:

Array
(   
    [0] => Sunday   
    [1] => Monday   
    [2] => Tuesday   
    [3] => Wednesday   
    [4] => Thursday   
    [5] => Friday   
    [6] => Saturday
)

[wpdm_file id=68]

How it works:
The explode() function split the string(second parameter – $language) into smaller pieces separated by the separator defined as the first parameter(, ).