Convert Comma Separated String into Array in PHP
Suppose you have a comma separated string which you want to convert into an array for any purpose. How will you do that? Here in this tutorial, I am going to give you the simple code snippet which will show you how to do that.
PHP code to convert comma separated string into array
Below is the given example PHP code:
<?php $string ="Raju,Anup,Irfan,Souvik"; $arr = explode(",",$string); print_r($arr); ?>
If you run the above code on a PHP server than you will able to see the output array that is given below:
Array (  => Raju  => Anup  => Irfan  => Souvik )
Now, you have seen that we have just converted a comma separated string into an array.
In the code, we use the PHP explode() function. explode function of PHP breaks a string into an array. Below is the syntax of this function:
the separator is required parameter. It specifies where to break the string.
This is also required parameter. This string will be split into an array.
This is the optional parameter that can be used with explode function. It specifies the number of array elements to return.
I hope, you have liked the code snippet in this post and it is helpful to you.