Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using implode for stdClass Objects in php

Tags:

php

implode

foreach($categories as $category)
{
    print_r($category);
}

The code above gives me the following result.

stdClass Object
(
    [category_Id] => 4
    [category_Title] => cat 4
)
stdClass Object
(
    [category_Id] => 7
    [category_Title] => cat 7
)
stdClass Object
(
    [category_Id] => 6
    [category_Title] => cat 6
)

how can I use implode(', ' ) to get the following result:

cat 4, cat 7, cat 6

I used it, but I got an error

like image 799
Afshin Avatar asked Nov 30 '13 06:11

Afshin


1 Answers

Here's an alternative solution using array_map:

$str = implode(', ', array_map(function($c) {
    return $c->category_Title;
}, $categories));
like image 141
Guilherme Sehn Avatar answered Oct 23 '22 05:10

Guilherme Sehn