PHP Array to string

Basically, the conversion from a PHP Array to a string can be easily done by the implode() function. So, here you will find some examples.


So, the variable $string has now the contents of the PHP Array. The implode() function usage is: implode(separator,array);

For example, some more ways on how you can convert a PHP array to string:

More info on the implode() function can be found here.

Multidimensional PHP Array to String

Maybe in case you need to convert a multidimensional array into a string, you may want to use the print_r() function. This is also called “array with keys”. By adding “true” as its second parameter, all the contents of the array will be cast into the string.


Then, the output:

More info on the PHP function print_r() can be found here.

Example: array to a comma-separated string

In this example, we will have a string with the contents of the array elements separated with commas.

The output is: “One,Two,Three,Four”.

Example: print array as a string

For this purpose, we will simply use the command print_r(). It will print the raw contents of an array (simple, multidimensional, with keys, etc) as a string.

Encode an array to a JSON string

For that purpose, we can use the command json_encode().

The value of $json_string is:

Leave a Reply

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