How to convert an Array to String in PHP?

In this article, you will learn how to convert an array into a string using PHP’s built-in function implode().
Syntax.

Convert an Array to String using PHP

The implode function takes two values as parameters.

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

glue – It’s a value that is concatenated to each element of the array when output as a string. This can be skipped. But if there is nothing to “glue,” the elements won’t have anything in between and appear adjacent.

array – It’s an array that you need to convert

<?php

$arr = ["pizza", "cake", "fruit salad"];
echo "Menu:" . implode($arr);

?>

Output:

As you see, the elements are glued together without any space between them.

<?php
$arr = ["pizza", "cake", "fruit salad"];
$glue = " , ";
echo "Menu: " . implode($glue, $arr);

?>

Output:

One Reply to “How to convert an Array to String in PHP?”

Leave a Reply

Your email address will not be published.