Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to convert the array to string using implode method in php my array is below format

$list =    Array
(
    [0] => Array
        (
            [name] => Abirami
        )

    [1] => Array
        (
            [name] => Amirtham
        )

    [2] => Array
        (
            [name] => Ganesh Cinemas
        )

    [3] => Array
        (
            [name] => Mathi
        )

    [4] => Array
        (
            [name] => Minipriya
        )

    [5] => Array
        (
            [name] => Saraswathi
        )

    [6] => Array
        (
            [name] => Sri Devi Kalaivani
        )

    [7] => Array
        (
            [name] => Suga priya
        )

)

I want the result in string Amirtham,Ganesh Cinemas etc.

$result =implode(",",$list);
share|improve this question
add comment

closed as off-topic by Jeremy, Mr. Alien, Dave Chen, tereško, PeeHaa Aug 3 '13 at 21:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – Jeremy, Mr. Alien, Dave Chen, tereško, PeeHaa
If this question can be reworded to fit the rules in the help center, please edit the question.

4 Answers

foreach($list as $row){echo $row['name'].',';}

or

$new_arr = array();
foreach($list as $row){$new_arr[] = $row['name'];}
echo implode(',',$new_arr);
share|improve this answer
add comment

You can use array_column() function as follow:

$results = implode(',',array_column($list, 'name'));
share|improve this answer
add comment

Another possibility would be to use array_map:

$result = implode(',', array_map(function($a) { return $['name']; }, $list));

Or if you're using PHP >= 5.5, you could use array_column:

$result = implode(',', array_column($list, 'name'));
share|improve this answer
add comment
// your list
$list = array( "0" => array("name" => "Abirami" ),
    "1" => array("name" => "Amirtham" ),
    "2" => array("name" => "Ganesh Cinemas" )); 

// empty string to store names
$result = null;

// foreach loop to store individual name from array into string variable
foreach($list as $l) {
$result .= $l['name'] . ",";        
}
echo $result;

Output

Abirami,Amirtham,Ganesh Cinemas,
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.