How to Convert Array to String in PHP?
Last Updated :
02 Jun, 2025
In PHP, Arrays are used to store multiple values in a single variable. However, sometimes we need to convert these arrays into strings:
- To display array data in a readable format.
- To join array items into a single sentence or list.
- To use the array data where only strings are accepted.
- To prepare data for storage in databases or files.
Common Methods to Convert Array to String in PHP
1. Using implode() Function
The most common and straightforward method to convert an array to a string is the implode() function. The implode() function joins array elements into a single string separated by the specified separator.
Now, let us understand with the help of the example:
PHP
<?php
$fruits = ['apple', 'banana', 'orange'];
$string = implode(", ", $fruits);
echo $string;
?>
Outputapple, banana, orange
2. Using join() Function
join() means the same as implode(). Both work in exactly the same way.
PHP
<?php
$fruits = ['apple', 'banana', 'orange'];
$string = join(" | ", $fruits);
echo $string;
?>
Outputapple | banana | orange
3. Using serialize() Function
serialize() converts the entire array (including keys and nested arrays) into a storable string representation. It is useful for storing or transmitting the complete array structure.
Now, let us understand with the help of the example:
PHP
<?php
$array = ['name' => 'anjali', 'age' => 30];
$serialized = serialize($array);
echo $serialized;
?>
Outputa:2:{s:4:"name";s:6:"anjali";s:3:"age";i:30;}
4. Using json_encode() Function
json_encode() converts an array into a JSON-formatted string. This method is very popular for data interchange between PHP and JavaScript or APIs.
PHP
<?php
$array = ['name' => 'anjali', 'age' => 30];
$json = json_encode($array);
echo $json;
?>
Output{"name":"anjali","age":30}
Best Practices
- Use implode() for simple arrays when you want a readable list.
- Use serialize() for storing PHP-specific array data (note: only PHP can unserialize it).
- Be careful about the types of data and special characters when changing arrays into strings.
- For multidimensional arrays, prefer serialize().
Conclusion
Converting arrays to strings in PHP is a common and essential task that can be accomplished in various ways depending on the specific need. For simple lists and readable output, functions like implode() or join() are straightforward and effective. When it comes to storing or transmitting complex array structures, especially multidimensional arrays, serialize() and json_encode() provide reliable solutions, with json_encode() also offering great compatibility for data exchange with other systems.
Similar Reads
How to Convert Byte Array to String in PHP? Given a Byte Array, the task is to convert the byte array to a String in PHP. It is used in various scenarios, such as processing binary data, handling file uploads, or working with data transmission protocols. Below are the approaches to convert byte array to string in PHP:Table of ContentWhat is a
3 min read
How to Convert Query String to an Array in PHP? In this article, we will see how to convert a query string to an array in PHP. The Query String is the part of the URL that starts after the question mark(?).Examples:Input: str = "company=GeeksforGeeks&address=Noida&mobile=9876543210"Output: Array ( [company] => GeeksforGeeks [ad
4 min read
How to Convert Integer array to String array using PHP? Given is an integer Array, the task is to convert the Integer array to a String array and print the output.Example:Input: $integerArray = [10, 20, 30, 40, 50];Output: ["10", "20", "30", "40", "50"]These are the following approaches:Table of ContentUsing the array_map() and strval() functionUsing a l
4 min read
How to Convert a String to JSON Object in PHP ? Given a String, the task is to convert the given string into a JSON object in PHP. JSON (JavaScript Object Notation) is a widely used data interchange format. PHP provides convenient functions to work with JSON data. Table of ContentUsing json_decode() to Convert to an ArrayHandling Errors with json
3 min read
How to add a Character to String in PHP ? Given two strings, the task is to add a Character to a String in PHP. The character can be added to a string in many ways in this article we will be using eight methods including the Concatenate Method, the String Interpolation Method, the str_pad() Method, Concatenation Assignment (.=) Operator, St
5 min read