DEV Community

Cover image for Convert an Array To String with PHP
Programming Dive
Programming Dive

Posted on

PHP Array to String Convert an Array To String with PHP

For more details, check details here

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
  • Using implode() function

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.

The 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);
Enter fullscreen mode Exit fullscreen mode
// 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));
Enter fullscreen mode Exit fullscreen mode
// 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;
Enter fullscreen mode Exit fullscreen mode
// OUTPUT

BMW, Ferrari, Honda

2. Using Associative array

<?php
$cars = array('BMW' => 'Germany', 'Ferrari' => 'Italy', 'Honda' => 'Japan');
$cars_together = implode(", ", $cars);
echo $cars_together;
Enter fullscreen mode Exit fullscreen mode
// 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));
Enter fullscreen mode Exit fullscreen mode
// OUTPUT

Germany, Italy, Japan

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.

Top comments (0)