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.
implode(string $separator, array $array): string
Return Value: Returns string value from elements of an array joined by separator.
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
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.
PHPAs the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him