How to Convert String into Array in PHP?

By Hardik Savani November 5, 2023 Category : PHP

In this short tutorial we will cover an how to convert string into array in php. step by step explain how to convert string value into array in php. I would like to share with you php convert string to array by comma. you'll learn php convert string to array with delimiter. follow bellow step for how to create array from string in php.

We will use explode() to convert string to array in PHP. explode() provide ways to convert a string into an array using a delimiter. I will give you two examples that way you can understand how it works.

Let's see below example:

Example 1:

index.php

<?php

$myString = "Hi, I am ItSolutionStuff.com";

$array = explode(' ', $myString);

print_r($array);

Output:

Array

(

[0] => Hi,

[1] => I

[2] => am

[3] => ItSolutionStuff.com

)

Example 2:

index.php

<?php

$myString = "Rajkot,Surat,Bhavnagar,Baroda";

$array = explode(',', $myString);

print_r($array);

Output:

Array

(

[0] => Rajkot

[1] => Surat

[2] => Bhavnagar

[3] => Baroda

)

I hope it can help you...

Tags :
Shares