How to do Array to String conversion in PHP
  John Mwaniki /   21 Dec 2021

How to do Array to String conversion in PHP

When working with arrays, it becomes inevitable to convert them into strings sometimes in order to perform certain actions.

In this article, we will cover two ways in which you can convert arrays into strings in PHP:

  1. Using the implode() or join() functions
  2. Using the json_encode() function

1. Using implode() or join() functions

The implode() is an inbuilt PHP function that is used to join the elements of an array together to form a string.

The implode() is an alias of the join() function, working exactly the same way and so you can use them interchangeably.

Syntax

implode(separator,array)

This function accepts its parameters in either order. However, it is recommended to always use the above order.

Parameters

Parameter Required Description
separator Optional Specifies what to put between the array elements while converting it to a string. Its default value is an empty string, ie. "". We can specify any value to be the separator eg. space " ", comma ",", colon ":", hyphen "-", etc.
array Required Specifies the array to join into a string.

Example 1

<?php
$array = array("Hello", "world.", "I", "like", "PHP.");
echo implode(" ", $array);

Output:

Hello world. I like PHP.

In the above example, we have converted an array named $array into a string and printed it on the screen to give the above output. We have used a space " " as the separator between the array elements in the resulting string.

Example 2

<?php
$laptops = ["HP", "Apple", "Lenovo", "Toshiba", "Dell", "Acer"];
var_dump(implode(", ", $laptops));

Output:

string(38) "HP, Apple, Lenovo, Toshiba, Dell, Acer"

The var_dump() function is used to display structured information about a variable such as its name and its value. In the above example, we have converted an array namely $laptops into a string using the implode() function and with a comma and space ", " as the separator of the array elements in the resulting string.

We have then displayed the output information using the var_dump function. As you can see, it's of the string type, comprising 38 characters.

In the same way, the array elements indices are not included in the resulting string for the indexed array, the array keys too for associative arrays are not included in the string. Only the values are used in making the string.

Example

<?php
$laptop = array("name" => "HP Folio 9470m", "ram" => "8Gb", "disk" => "500Gb SSD", "processor" => "Core i7");
var_dump(implode(" ", $laptop));

Output:

string(36) "HP Folio 9470m 8Gb 500Gb SSD Core i7"

2. Using the json_encode() function

The json_encode() function is used in PHP to convert an array into a JSON string.

Example 1

<?php
$user = array("first_name" => "John", "last_name" => "Doe","age" => 28, "gender" => "male");
echo json_encode($user);

Output:

{"first_name":"John","last_name":"Doe","age":28,"gender":"male"}

Example 2

<?php
$cast = array("Klaus", "Elijah", "Hayley", "Marcel", "Rebekah", "Davina");
echo json_encode($cast);

Output:

["Klaus","Elijah","Hayley","Marcel","Rebekah","Davina"]