How to convert an array to a string in PHP?

Before knowing how to convert from an array to a string in PHP, we will know what array and string are in PHP. String and Array are built-in data types of PHP.

What is a string?

A string is a collection of characters (character is equivalent to a byte).

Example of a string

An example of string is:

$color = 'blue';

We can learn more about strings in PHP at: https://www.php.net/manual/en/language.types.string.php.

What is an array?

An array stores a number of values in one single variable.

Example of an array

An example of an array is:

$colors = ['blue', 'green', 'red'];

We can learn more about arrays in PHP at: https://www.php.net/manual/en/language.types.array.php.

Convert array to string in PHP

We will now know how to convert from array to string in PHP.

For example, we will convert the above-mentioned array to a string and the output will be: blue, green, red.

Processes to convert an array to a string in PHP

For this, we can follow the following processes.

Process 1: Using implode or join function.

implode or join functions join array elements to a string.

<?php
$colors = ["blue", "green", "red"];
$stringColor = implode(", ", $colors);
echo $stringColor;
?>
<?php
$colors = ["blue", "green", "red"];
$stringColor = join(", ", $colors);
echo $stringColor;
?>

Here the output is: blue, green, red

The first parameter of these functions is a separator. If we do not add any separator like:
$stringColor = implode("", $color);
, then the output will be: bluegreenred

Process 2: We can loop through the array and create the desired string.
<?php
$colors = ["blue", "green", "red"];
$stringColor = "";
if(!empty($colors)){
foreach($colors as $val){
$stringColor .= $val.", ";
}
}
echo substr($stringColor,0,strlen($stringColor)-2);
?>

Here the output is: blue, green, red

Beyond the above-mentioned two processes, we can convert array to string but in that case, the output string will not be the same as above mentioned 2 processes.

Process 3: Using json_encode function.

json_encode function returns the JSON representation of a given value. We can use the json_encode function like the below code:

<?php
$colors = ["blue", "green", "red"];
$stringColor = json_encode($colors);
echo $stringColor;
var_dump($stringColor);
?>

The output is: [“blue”,”green”,”red”]

The output is actually a string. If we use var_dump we can see this: string(22) “[“blue”,”green”,”red”]”

# var_dump function is used to dump information about a variable. We can We can learn more about this at: https://www.php.net/manual/en/function.var-dump.php.

json_decode is used to decode a JSON string. Like if we want to decode the JSON-encoded string we can write the below code:

<?php
$encodedColor = '["blue","green","red"]';
$colors = json_decode($encodedColor);
var_dump($colors);
?>

Here, if we use var_dump, we get the output: array(3) { [0]=> string(4) “blue” [1]=> string(5) “green” [2]=> string(3) “red” }

We can learn more about json_encode at: https://www.php.net/manual/en/function.json-encode.

We can learn more about json_decode at: https://www.php.net/manual/en/function.json-decode.php.

Process 4: Using serialize function.

serialize function is used to generate a storable representation of a given value. We can use the json_encode function like the below code:

<?php
$colors = ["blue","green","red"];
$stringColor = serialize($colors);
echo $stringColor;
?>

Here, the output is: a:3:{i:0;s:4:”blue”;i:1;s:5:”green”;i:2;s:3:”red”;}

We can use the unserialize function to generate a PHP value from a serialized value.

<?php
$serializedColor = 'a:3:{i:0;s:4:"blue";i:1;s:5:"green";i:2;s:3:"red";}';
$colors = unserialize($serializedColor);
var_dump($colors);
?>

With this we see that we get back the array as we see if we use var_dump function we get: array(3) { [0]=> string(4) “blue” [1]=> string(5) “green” [2]=> string(3) “red” }

serialize function is useful to store or pass the PHP values as it doesn’t lose their type and structure.

Especially since the array can’t be saved in mysql so we can save serialized data. Again during fetch, we can unserialize and get back the array.

We can learn more about serialize at: https://www.php.net/manual/en/function.serialize.

We can learn more about unserialize at: https://www.php.net/manual/en/function.unserialize.

2 thoughts on “How to convert an array to a string in PHP?”

  1. Pingback: Webtechbased
  2. Pingback: Webtechbased

Comments are closed.