View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
We have given an array and the task is to convert the array elements into string. In this article, we are using two methods to convert array to string.
Method 1: Using implode[] function: The implode[] method is an inbuilt function in PHP and is used to join the elements of an array. The implode[] method is an alias for PHP | join[] function and works exactly same as that of join[] function.
Syntax:
string implode[$separator, $array]
Example:
PHP
Output:
Welcome to GeeksforGeeks A Computer Science Portal
Method 2:
Using json_encode[] Function:The json_encode[] function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation.
Syntax:
string json_encode[ $value, $option, $depth ]
Example:
PHP
Output:
{"name":"GFG","0":{"email":"","mobile":"XXXXXXXXXX"}}
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
What is the best method for converting a PHP array into a string?
I have the variable $type
which is an array of types.
$type = $_POST[type];
I want to store it as a single string in my database with each entry separated by |
:
Sports|Festivals|Other
mickmackusa
39k11 gold badges76 silver badges112 bronze badges
asked Sep 20, 2011 at 19:13
Philip KirkbridePhilip Kirkbride
20.2k34 gold badges114 silver badges216 bronze badges
5
Use implode
implode["|",$type];
answered Sep 20, 2011 at 19:14
3
You can use json_encode[]
Later just use json_decode[] to decode the string from your DB. Anything else is useless, JSON keeps the array relationship intact for later usage!
answered Sep 20, 2011 at 19:16
7
json_encode[$data] //converts an array to JSON string
json_decode[$jsonString] //converts json string to php array
WHY JSON : You can use it with most of the programming languages, string created by serialize[] function of php is readable in PHP only, and you will not like to store such things in your databases specially if database is shared among applications written in different programming languages
answered Oct 3, 2013 at 10:46
Sumit KumarSumit Kumar
1,72717 silver badges19 bronze badges
3
One of the Best way:
echo print_r[$array, true];
T.Todua
50.1k19 gold badges216 silver badges213 bronze badges
answered Jun 18, 2014 at 11:24
4
No, you don't want to store it as a single string in your database like that.
You could use serialize[]
but this will make your data harder to search, harder to work with, and wastes space.
You could do some other encoding as well, but it's generally prone to the same problem.
The whole reason you have a DB is so you can accomplish work like this trivially. You don't need a table to store arrays, you need a table that you can represent as an array.
Example:
id | word
1 | Sports
2 | Festivals
3 | Classes
4 | Other
You would simply select the data from the table with SQL, rather than have a table that looks like:
id | word
1 | Sports|Festivals|Classes|Other
That's not how anybody designs a schema in a relational database, it totally defeats the purpose of it.
answered Sep 20, 2011 at 19:14
IncognitoIncognito
20.2k15 gold badges77 silver badges119 bronze badges
9
implode[]: