In this article, you will learn how to convert an array into the 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 these.

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

?>

Output:

Last modified: August 1, 2020

Author

Comments

Write a Reply or Comment

Your email address will not be published.