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:

$string ="Raju,Anup,Irfan,Souvik";
$arr = explode(",",$string);

If you run the above code on a PHP server than you will able to see the output array that is given below:

Array ( [0] => Raju [1] => Anup [2] => Irfan [3] => 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.

Process form value in PHP using jQuery AJAX method

Replace A Character Of String Using PHP With Other Character


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.

One response to “Convert Comma Separated String into Array in PHP”

  1. Umalac Hasum says:

    This saved me a lot of time. Simple but powerful.

Leave a Reply

Your email address will not be published. Required fields are marked *