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:
TABLE OF CONTENT 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.
echoimplode(" ",$myarr);// Use of implode function
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:
echoimplode(', ',$myarr);// 1, , 0, 1, , 4.32
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.
$mystr="Every moment is a fresh beginning";
//explode function breaks an string into array
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 (  => Every  => moment  => is  => a  => fresh  => 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.
//Assigning values to the object variable
//json_encode() put data into associative array with index
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.
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:
$mystr=rtrim($mystr,$sep);// remove last separator
// $brands array from example above
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 email@example.com I will try to resolve the problem.
Stay healthy, stay safe! Happy coding! enjoy it!