How to Convert PHP Array to String Text?

Sovary June 10, 2022 362
1 minute read

This short tutorial, I will show you how to convert an array to string in PHP. The example below will help you change array into string by separately with space or any characters as delimiter. I will explain very simple and quick guide to convert an array to string with space separated.

In PHP, we are going to use implode() function to convert an array to a string type with a delimiter. I'll give you two examples below so that you will understand how it works.

implode() - Allow to join each data elements into a list of string using a separator or delimiter.

Syntax

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

Return Value: Returns string value from elements of an array joined by separator.

Parameters

Parameter Type Description
$separator String (Optional) Character or symbols to place between earch array items by default is an empty string.
$array Array (Required) One demensional array to join together.

Also Read

Examples

Example #1

<?php
$fruits = ["Banana", "Apple", "Orange", "Grape", "Mango"];
$string = implode(' ', $fruits );
var_dump($string);
?>

Output:

string(31) "Banana Apple Orange Grape Mango"

 Example #2

<?php
$num= [1, 2, 5, 9, 7, 5];
$string = implode('-', $num);
var_dump($string);
?>

Output:

string(11) "1-2-5-9-7-5"

I hope you will learn and how to use implode function to combine elements of array into a single string with seperator.

PHP 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him