Convert an Array To String with PHP

Convert an Array To String with PHP

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.

Using json_encode() function to convert an Array to a string

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.

Syntax for using this function remains very basic-

json_encode ( mixed$value [, int $options = 0 [, int $depth = 512 ]] ) : string|false

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

$cars = array(
  array(
    'BMW' => 'Germany'
  ),
  array(
    'Ferrari' => 'Italy'
  ),
  array(
    'Honda' => 'Japan'
  )
);
echo json_encode($cars);
// OUTPUT

[{"BMW":"Germany"},{"Ferrari":"Italy"},{"Honda":"Japan"}]

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

var_dump(json_encode($cars));
// OUTPUT

string(57) "[{"BMW":"Germany"},{"Ferrari":"Italy"},{"Honda":"Japan"}]"

Using implode function to convert an array to a string

The syntax for implode function is quite simple-

implode ( string $glue , array $pieces ) : string

Here,

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

$pieces: 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.

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

echo $cars_together;
// OUTPUT

BMW, Ferrari, Honda
2. Using Associative array
<?php

$cars = array('BMW' => 'Germany', 'Ferrari' => 'Italy', 'Honda' => 'Japan');
$cars_together = implode(", ", $cars);

echo $cars_together;
// OUTPUT

Germany, Italy, Japan

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.

3. Using Multidimensional Array

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));
// OUTPUT

Germany, Italy, Japan

Using Serialize function to convert an array to a string

serialize() function in PHP is used to returns a string containing a byte-stream representation of any value that can be stored in PHP. serialize is used to save all variables in an object.

To serialize any value we need to pass the value to the function and in return, it will return the byte-stream representation of that value.

<?php

$automobile = array(
  array(
    'BMW' => 'Germany'
  ),
  array(
    'Ferrari' => 'Italy'
  ),
  array(
    'Honda' => 'Japan'
  )
);

print_r(serialize($automobile));
// OUTPUT

a:3:{i:0;a:1:{s:3:"BMW";s:7:"Germany";}i:1;a:1:{s:7:"Ferrari";s:5:"Italy";}i:2;a:1:{s:5:"Honda";s:5:"Japan";}}

Let’s break the serialized string.

a:3 -> says that we have 3 main elements
i:0/i:1/i:2 -> says first/second and the third element respectively
a:1 -> denotes the number of elements like we saw a:3
s:3/s:7 -> denotes the length of the string. For each data type, there will be different lengths.

Conclusion:

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.