Convert Array to String and Convert String to Array in Php

explode () and implode () methods are used for this process.

Using the explode () method:

The explode () method is used to convert the string to array. The explode () method takes 2 parameters. The first parameter is the parameter according to which character should split the string. The second parameter is the string value to be split.

<?php
$value= "Hello World I am Learning Php";
$words= explode(" ", $value);
echo $words[0]; 
echo $words[1]; 
echo $words[2]; 
echo $words[3];
?>
output:
Hello 
World 
I
am 
Learning 
Php

Using the implode () method:

The implode () method is used to convert the string to string. The implode () method takes 2 parameters. The first parameter specifies which character to put in between. the second parameter is the value of the string to be split.

<?php
$value= array('Hello', 'World', 'I','am', 'learning', 'Php');
$stringvalue= implode(" ", $value);
echo $stringvalue; 
?>

output :

Hello World I am Learning Php

 

Tagged In:

Software developer

26 Total Posts