How To Convert Comma Separated String into Array?

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 ( [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:

explode(separator,string,limit)

separator:

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

string:

This is also required parameter. This string will be split into an array.

limit:

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.

Leave a Reply

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