How to Convert PHP Array To String With Example

Summary: In this article, you will learn how to convert PHP array to string with example. furthermore, also learn the various ways that you can convert PHP array to string in PHP. let’s understand this article in details.

To Convert PHP Array To String Overview:

This tutorial covers a two ways regarding to convert array to string:

1. Using implode( ) function
2. Using explode( ) function
3. Using json_encode( ) function

1. Using implode( ) function

By using the PHP implode() function, we can convert all array elements into a string. This function returns the string. The separator parameter in implode() function is optional.

As you can see in the above example, at the first line, an array variable is declared and assigned some values.
Furthermore, in the next line, the PHP implode() function will convert the array into a string. there are two parameters passed in the implode() function. 1st is the separator and the other one is array_name.

Every moment is a fresh beginning

The implode function in PHP returns a string consisting of array element values joined using a string that you just specify:

Here, the string used to separate the array values is the 1st argument. If you leave out that argument and just simply pass the array you want to join, the string between array elements defaults to an empty string:

Furthermore, suppose that if arrays consist of values that aren’t strings, thus those values will be converted to strings for inclusion in the string returned by the PHP implode function:

Important Note: Return value: numbers are measure accurately represented in the string returned by the PHP implode function while true is converted to one (1), and false and NULL show as empty strings.

2. Using explode() function

By using PHP explode() function, you can convert a string into array elements. here, you can pass three arguments in it. The 1st one is for separator, the second for array_name, and the last one for the limit.

Therefore, as you can see in the above example, a string variable is assigned a few values. Then, PHP explode() function breaks this string into an array. furthermore, we used print_r() function which prints all the array elements and their indexes.

Array ( [0] => Every [1] => moment [2] => is [3] => a [4] => fresh [5] => beginning )

3. Using json() Function

In PHP, the objects can be converted into JSON String by using the json_encode() function. furthermore, common use of JSON is to read data from a web server and display the data on a web page.

therefore, as you can see in the above example, we assigned the value to the object variable and then in json_encode()converts the value to the array variable and makes the associative array.


PHP offers a lot of ways in which to convert arrays and other values to strings: json_encode and serialize. 1st, we demonstrate how to applying json_encode to the following array:


Furthermore, next we pass the same or identical array to the serialize function:


PHP Array to String Question and Answer

Question: How to Convert Array of Arrays to String in PHP

Answer: Suppose that if the array we pass to the implode function contains elements that are arrays, then Array will be the output for each sub-array:

Also, an error will be logged or displayed: Notice: Array to string conversion.
An array consisting of a single level of sub-arrays, like the example above, could be converted to a string using the following function:

Arrow, Peter England, Van Heusen, Louis Phillipe, Nike, adidas, Bata, Piaget, Harry Winston, Benson


In this article, you have learned various ways to convert PHP array to string with example. I hope you will enjoy it!. if have any query related program then contact me at I will try to resolve the problem.
Stay healthy, stay safe! Happy coding! enjoy it!