Converting Array to String in PHP

To convert an array to a string in PHP you can use implode($separator, $array), json_encode($array) or serialize($array) functions. The implode() function allows you to concatenate array elements into a single string. To do this, pass an array, and a delimiter to the implode(); the separator will be inserted between the array values. The json_encode() function converts an array to a JSON string. It takes an array as input and returns a JSON array representation. The serialize() function takes an array and converts it to a string. This function is handy if you want to store something in the database and retrieve it for later use. In this Convert PHP Array to String example, we use the implode() function to convert an array to a string. Below you can see more examples of converting a PHP array to a string with a detailed description of each method. Click Execute to run the PHP Array to String Example online and see the result.
Converting Array to String in PHP Execute
$array = array('PHP','array', 'to', 'string', 'conversion', 'example');
echo implode(' ', $array);
Updated: Viewed: 3347 times

What is PHP?

PHP is a server-side scripting language used for web development. PHP is an open-source general-purpose language that can be embedded in HTML code. PHP creates dynamic web pages, processes forms, manages databases, and performs other web-related tasks. PHP is easy to learn and has a large developer community. PHP is a cross-platform language, allowing it to run on various operating systems such as Linux, Windows, macOS, and Unix.

What is an array in PHP?

In PHP, an array is a variable that holds multiple data elements. Each element within an array is identified by a unique index or key, which can be either a number or a string. There are two types of arrays in PHP: indexed arrays and associative arrays. PHP also includes numerous built-in functions for handling arrays, such as searching, sorting, converting array to JSON, splitting a string to array, and determining the length of an array.

What is a string in PHP?

PHP strings are sequences of characters, where each character is represented by a single byte. A byte can only contain 256 characters, and PHP has no native support for Unicode strings. In PHP, you can create strings using single quotes ('...'), double quotes ("..."), and Heredoc syntax (<<<). PHP provides a rich set of built-in functions for replacing, concatenating, comparing, interpolating, and splitting strings. You can also find the length of a string, convert the string to int, and convert the string to array.

Converting array to a string using the implode()

To convert an array to a string using PHP implode() function, you must pass a delimiter as the first argument and an array as the second.

PHP implode() Syntax
implode(separator, array)

  • separator (optional): character to be inserted between array elements. The default is an empty string.
  • array: array to convert to string
Convert PHP array to string using the implode()
$array = array('I','like', 'PHP');
echo implode('-', $array);

#output: I-like-PHP

Converting array to JSON string using json_encode()

To convert an array to a JSON string with PHP, you can use the json_encode() function, which takes an array as input and converts it to JSON.

Convert PHP array to JSON string Example
$array = array(
    'Name' => 'Leo',
    'Age' => 25,

echo $json = json_encode($array); 

#output: {"Name":"Leo","Age":25}

Converting array to string using serialize()

The serialize() function is used a variable (in our case, an array) into a storable state. The function returns the array to a string, which can then be converted back to an array in the future.

PHP Array serialize() Example
$array = array('apple', 'orange');

echo $string = serialize($array); ;

#output: a:2:{i:0;s:5:"apple";i:1;s:6:"orange";}

See also