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.
Syntax:
1 2 3 4 5 |
<?php //assigning value to the array $myarr = array("Every","moment","is","a", " ", "fresh","beginning"); echo implode(" ",$myarr);// Use of implode function ?> |
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.
The implode function in PHP returns a string consisting of array element values joined using a string that you just specify:
1 2 3 4 5 |
<?php $myarr = ['Cauliflower', 'Carrot', 'Capsicum', 'Cabbage']; echo implode(', ', $myarr); // Cauliflower, Carrot, Capsicum, Cabbage ?> |
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:
1 2 3 4 |
<?php $myarr = ['m', 'n', 'o', 'p', 'q', 'r', 's']; echo implode($myarr); // mnopqrs ?> |
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:
1 2 3 4 |
<?php $myarr = [true, false, 0, 1, NULL, 4.31]; echo implode(', ', $myarr); // 1, , 0, 1, , 4.32 ?> |
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.
Syntax:
1 2 3 4 5 6 |
<?php $mystr="Every moment is a fresh beginning"; //explode function breaks an string into array $myarr=explode(" ",$mystr); print_r($myarr); ?> |
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.
Syntax:
1 2 3 4 5 6 7 8 9 10 |
<?php //Assigning values to the object variable @$myObject->name="Aditya"; @$myObject->age=30; @$myObject->city="Lucknow"; @$myObject->country="India"; //json_encode() put data into associative array with index $myJSON=json_encode($myObject); echo($myJSON); ?> |
{"name":"Aditya","age":30,"city":"Lucknow","country":"India"}
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:
1 2 3 4 5 6 7 8 9 10 |
<?php $student = [ 'name' => 'Pinki', 'age' => 30, 'husband' => 'aditya', 'status' => null, 'friends' => ['John', 'arti', 'dyana'] ]; echo json_encode($student); ?> |
{"name":"Pinki","age":30,"husband":"aditya","status":null,"friends":["John","arti","dyana"]}
Furthermore, next we pass the same or identical array to the serialize function:
1 2 3 4 5 6 7 8 9 10 |
<?php $student = [ 'name' => 'Pinki', 'age' => 30, 'husband' => 'aditya', 'status' => null, 'friends' => ['John', 'arti', 'dyana'] ]; echo serialize($student); ?> |
a:5:{s:4:"name";s:5:"Pinki";s:3:"age";i:30;s:7:"husband";s:6:"aditya";s:6:"status";N;s:7:"friends";a:3:{i:0;s:4:"John";i:1;s:4:"arti";i:2;s:5:"dyana";}}
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:
1 2 3 4 5 6 7 8 9 |
<?php $brands = [ 'shirts' => ['Arrow', 'Peter England', 'Van Heusen', 'Louis Phillipe'], 'shoes' => ['Nike', 'adidas', 'Bata'], 'watch' => ['Piaget', 'Harry Winston', 'Benson'] ]; echo implode(', ', $brands); // Array, Array, 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $brands = [ 'shirts' => ['Arrow', 'Peter England', 'Van Heusen', 'Louis Phillipe'], 'shoes' => ['Nike', 'adidas', 'Bata'], 'watch' => ['Piaget', 'Harry Winston', 'Benson'] ]; function subArraysToString($myarr, $sep = ', ') { $mystr = ''; foreach ($myarr as $val) { $mystr .= implode($sep, $val); $mystr .= $sep; // add separator between sub-arrays } $mystr = rtrim($mystr, $sep); // remove last separator return $mystr; } // $brands array from example above echo subArraysToString($brands); ?> |
Conclusion:
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 info@tutorialscan.com I will try to resolve the problem.
Stay healthy, stay safe! Happy coding! enjoy it!