Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge array items into string

Tags:

arrays

string

php

How do I merge all the array items into a single string?

like image 516
James Avatar asked Jan 07 '11 14:01

James


2 Answers

Use the implode function.

For example:

$fruits = array('apples', 'pears', 'bananas');
echo implode(',', $fruits);
like image 141
cristian Avatar answered Oct 06 '22 03:10

cristian


Try this from the PHP manual (implode):

<?php
    $array = array('lastname', 'email', 'phone');
    $comma_separated = implode(",", $array);

    echo $comma_separated; // lastname, email, and phone

    // Empty string when using an empty array:
    var_dump(implode('hello', array())); // string(0) ""
?>
like image 32
Diablo Avatar answered Oct 06 '22 05:10

Diablo