Convert an Array To String with PHP
Tags: php, array, string
Published: 2020-11-19 07:50:55

For more details, <a href="https://programmingdive.com/convert-an-array-to-string-with-php/">check details here</a><br>
To convert an Array to String with PHP, we can use two different built-in functions available in PHP. These two functions only take one array at a time to convert it into a string.
<ul>
<li>Using json_encode() function</li>
<li>Using implode() function</li>
</ul>

<h3>Using json_encode() function to convert an Array to a string</h3>
To convert an array to a string, one of the common ways to do that is to use the json_encode() function which is used to returns the JSON representation of a value.

The syntax for using this function remains very basic-
<blockquote>
json_encode ( mixed$value [, int $options = 0 [, int $depth = 512 ]] ) : string|false
</blockquote>
This function takes any value as input except resource. But in our case, we’ll use an Array as an input value, and then this function will convert it into a JSON representation of the provided value.
```php
<?php
$cars = array(
  array(
    'BMW' => 'Germany'
  ),
  array(
    'Ferrari' => 'Italy'
  ),
  array(
    'Honda' => 'Japan'
  )
);
echo json_encode($cars);
```
<pre>
// OUTPUT

[{"BMW":"Germany"},{"Ferrari":"Italy"},{"Honda":"Japan"}]
</pre>
At first glance, it doesn’t look like a string but this how JSON looks like. If we use var_dump() over json_encode() then it will show its data type as a string
```php
var_dump(json_encode($cars));
```
<pre>
// OUTPUT

string(57) "[{"BMW":"Germany"},{"Ferrari":"Italy"},{"Honda":"Japan"}]"
</pre>
<h3>Using implode function to convert an array to a string</h3>
The syntax for implode function is quite simple-
<blockquote>
implode ( string $glue , array $pieces ) : string
</blockquote>
Here,

<code>$glue:</code> is the string / special character(s) used to concatenate array values. Default is an empty string.

<code>$pieces:</code> is the array whose values will stick together using glue.

This will return all the Array elements concatenated together using glue in the same sequential order in which they appear in the array.

<h4>1. Using Indexed Array</h4>
```php
<?php
// using indexed array
$cars = array('BMW', 'Ferrari', 'Honda');
$cars_together = implode(", ", $cars);
echo $cars_together;
```
<pre>
// OUTPUT

BMW, Ferrari, Honda
</pre>

<h4>2. Using Associative array</h4>
```php
<?php
$cars = array('BMW' => 'Germany', 'Ferrari' => 'Italy', 'Honda' => 'Japan');
$cars_together = implode(", ", $cars);
echo $cars_together;
```
<pre>
// OUTPUT

Germany, Italy, Japan
</pre>
As mentioned above, all the values from the array will be stick together. Hence, if there is a situation where we need to get the values of the associative array to be glued together then use the same function.

<h4>3. Using Multidimensional Array</h4>
A multidimensional array can be simple OR complicated depending upon the situation in the project requirement. We will see the basic multidimensional array and for that, we need to write a callback function that will concatenate the values.
```
<?php
$automobile = array(
  array(
    'BMW' => 'Germany'
  ),
  array(
    'Ferrari' => 'Italy'
  ),
  array(
    'Honda' => 'Japan'
  )
);
echo implode(', ', array_map(function ($entry) {
  return ($entry[key($entry)]);
}, $automobile));
```
<pre>
// OUTPUT

Germany, Italy, Japan
</pre>
<h3>Conclusion:</h3>
We can use any of the methods to fulfill the requirement. But when need to decide either to use json_encode() OR serialize(), I would always suggest using json_encode() because it takes smaller storage space as compared to serialize.