How to convert array to string with PHP implode function

PHP implode function converts array to string in PHP, a data type conversion you’ll often see while coding. Data type conversions are pretty commonplace in any programming language. Some modern programming languages like Javascript do many conversions implicitly, making life easy for developers and programmers. On the contrary, in Java or C++, type conversions are rarely implicit.

In a similar context, PHP is also quite flexible and convenient when it comes to type conversion. In this article, we’ll see a data type conversion in PHP to convert array to string in PHP. Fortunately, the PHP implode function does a lot of work under the hood to make this conversion happen.

First, we’ll see the syntax of the PHP implode function, then some examples, and finally a practical scenario to understand the context where this conversion is helpful.

Syntax of PHP implode function

Description

PHP implode function joins array elements with a string.

Function Signature

implode(string $separator,array $array) : string

Arguments

  • $separator – A string to separate the elements. This argument is optional.
  • $array        – Array to be converted.

Return Type

  • string

Examples of PHP implode function

Let’s see PHP implode function with different examples.

String arrays

Let’s convert array to string using PHP implode function. Here we have an array of strings for programming languages. The example converts it into a string separating it by a comma. That’s why we pass comma as the first argument and array as second, according to what we’ve seen in the syntax.

<?php
//An array to be converted into a string.
$programmingLanguagesArray = ["PHP","Python","Javascript","Java","C++","R"];
 
//convert $programmingLangugaes array into string using the implode function.
$programmingLanguagesString = implode(", ",$programmingLanguagesArray);
 
//Echo $programmingLanguagesString to console.
echo $programmingLanguagesString;
 
//OUTPUT
//PHP, Python, Javascript, Java, C++, R
?>

Mixed Arrays

The function works equally well for arrays with mixed data types. Keep in mind that boolean type true is implicitly type-casted to 1, and likewise false to an empty string. See boolean type casting for clarity.

<?php
//A mixed array to be converted into a string.
$mixedArray = [1,2,"Three",4,"Five",false,true];
 
//Converts $mixedArray into string using the implode function.
$resultantString = implode(", ",$mixedArray);
 
//Echo $resultantString to console.
echo $resultantString;
 
//OUTPUT
//1, 2, Three, 4, Five, , 1
?>

Multi-dimensional Arrays

Arrays within arrays throw PHP warning on implode. That’s because implode tries to type-cast elements to string, and the array cannot be explicitly type-casted this way, that’s the reason why we use implode in the first place.

<?php
//A mutli-dimensional array to be converted into a string.
$multidimensionalArray = array(["Array","Within","Array"]);
 
//Converts $multidimensionalArray into string using the implode function.
$resultantString = implode(", ",$multidimensionalArray);
 
//Echo $resultantString to console.
echo $resultantString;
 
//OUTPUT
//PHP Warning:  Array to string conversion 
?>

Associative Arrays

Passing an associative array to implode converts the values only and leaves the keys out. The example shows verify it.

<?php
//An associative array to be converted into a string.
$associativeArray = array("Product"=>"Cereal","Price(USD)"=>16,"Available"=>true);
 
//Converts $associativeArray into string using the implode function.
$resultantString = implode(", ",$associativeArray);
 
//Echo $resultantString to console.
echo $resultantString;
 
//OUTPUT
//Cereal, 16, 1
?>

An easier workaround is to use array_keys function along with implode to get the keys.

<?php
//An associative array to be converted into a string.
$associativeArray = array("Product"=>"Cereal","Price(USD)"=>16,"Available"=>true);
 
//Converts keys of the $associateArray into string using the implode function.
$resultantStringKeys = implode(", ",array_keys($associativeArray));
 
 
//Converts values of the $associateArray into string using the implode function.
$resultantStringValues = implode(", ",$associativeArray);
 
//Echo $resultantStringKeys to console.
echo $resultantStringKeys."\n";
//Echo $resultantStringValues to console.
echo $resultantStringValues;
 
//OUTPUT
//Product, Price(USD), Available
//Cereal, 16, 1
?>

PHP implode function: A Grocery List (Scenario)

 Let’s assume an array of grocery items. We want to print this to console in a list format. One way to go about this problem is to loop through an array. That works too, but a more efficient approach uses implode to format the list out so PHP could print it out as a list.

This is possible if we pass “\n” as the separator argument. “\n” is a new line character that does all the magic and we get grocery items listed down in the console.

<?php
//An array for grocery items
$groceryArray = ["Cereals","Eggs","Dairy","Oil","Sauces","Fruits","Vegetables"];
 
 
//Converts it into a string using implode.
$groceryItems = implode("\n",$groceryArray);
 
//Prints the grocery items to console.
echo "The grocery items are:\n";
echo $groceryItems;
 
 
// OUTPUT
// The grocery items are:
// Cereals
// Eggs
// Dairy
// Oil
// Sauces
// Fruits
// Vegetables
 
?>

That’s convenient than a loop. We hope that you’re now comfortable with the implode function. Stay tuned for more helpful articles on PHP.

Want to explore further about PHP arrays?

We have many fun articles related to PHP arrays. You can explore these to learn more about arrays in PHP.

Make an associative array in PHP

Sort an array in PHP

Print an array in PHP


Article Categories

Article Tags

    © 2023 Confident.Systems