How to Convert array into string in PHP
While working with PHP, many times the developer requires converting the array data into string so they can easily implement the String functions to the data.
In this tutorial, we will briefly discover the various ways of converting Array into String in PHP.
PHP provides different inbuilt functions to help the programmers for converting an Array to String. Both the built-in functions only take one array at a single time and automatically convert the array data into a string.
PHP json_encode() function
In PHP, the json_encode() is the most commonly used function to convert an array to a string. This function returns the JSON value of the given array. The json_encode() function accepts an element as input except the resource values.
json_encode ( mixed$value [, int $options = 0 [, int $depth = 512 ]] ) : string|false
Example 1: In the below example, we have taken a multidimensional Array as input data. Then we have applied the json_encode() function to convert it into a string or a JSON value of the given array.
Though, if you look at the output, it doesn't look like a string, but that's how JSON's output looks. Further, if the programmer use the var_dump() function over json_encode() then it will display the values as a string.
PHP implode() function to convert an array to a string
The PHP implode() function accepts the input as an array and further converts it to a string. This function uses the glue parameter, which also acts as the separator for the given array argument. Therefore this function accepts the array, converts its values into a string, and concatenates them using a separator.
$glue: This parameter accepts string values / special character(s) that are further used to concatenate array values. By default it accepts an empty string.
$pieces: This parameter represents an array whose values stick together using glue.
The PHP implode() function will return all the Array values that are joined together using the glue in the exact sequential order in which they were present in the given array.
Example 1: Below given is the demonstration of code for converting array into string using implode() function.
The converted string is = Welcome to JavaTpoint
Example 2: Below given is the demonstration of code of converting a Indexed Array to string using implode() function.
Samsung, iPhone, Tata
Example 3: Below given is the demonstration of code of converting an associative Array to string using implode() function.
South Korea, India, American
As mentioned above, all the values from the array will be stick together. Therefore, if there exists a position where the user wants to glue together the fetched element of the associative array, in that case, he/she can use the same function.
Example 4: Below given is the demonstration of code of converting a multidimensional Array to string using implode() function.
A multidimensional PHP array can be either simple or complex, depending upon the project's needs and requirements. In the below example, we will look at the basic multidimensional array and a callback function that will help to concatenate the elements.
South Korea, India, American