문제

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

도움이 되었습니까?

해결책 3

Try like

foreach($categories as $category)
{
    $new_arr[] = $category->category_Title;
}
$res_arr = implode(',',$new_arr);
print_r($res_arr);

다른 팁

You may easily do this just casting as an array. Most people seem to have skipped college or C programming.

implode(',',(array) $categories); 

check this thread if you doubt me Convert PHP object to associative array

Here's an alternative solution using array_map:

$str = implode(', ', array_map(function($c) {
    return $c->category_Title;
}, $categories));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top