Faster PHP Cloud Hosting

Yes, I know that you are writing some php code and you need to convert an php array to string. Therefore I know that you don’t have much time so the direct answer to your question is the php implode() function.

For all those who are quick learners here  is a example for you.

PHP Array To String Conversion Example


$array = array("one","two","three","four","five");

$string = implode(" - ",$array) ;

echo $string ; // This will output as one - two - three - four - five


Many of you web developers might have understood it But in case if you didn’t understand it don’t worry I will be talking about this in brief further

How to use the PHP implode() function

Before proceeding further first let us look at the usage of the implode function.


implode(seperator, array);


ParameterTypeData TypeDescription
separatoroptimalstringthe separator which should be used to separate the elements in a string
arrayrequiredarrayAn array of elements which you want to convert in to string

NOTE : The separator parameter is optimal but I would recommend you to use it for backward compatibility.

Now you know how to use the php implode() function so we will now look at example with explanation so that you understand it well.

PHP Array To String Conversion Using PHP implode() Function Example With  Explanation


// Creating an array

$myarray = array('element1','element2', 'element3','element4');

// calling the implode function by passing the selector as the first parameter and the array variable as the second

$tostring = implode(' - ', $myarray);

// echoing the string we got by calling the php implode function();

echo #tostring ;


output :-

element1 - element2 - element3 - element4

I think you may have now understood how to convert a php array into string. In case if you didn’t get anything feel free to ask me in the comments section below.

So if you liked the post please don’t forget to like it and also share it with your other developer friends.