Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Concatenate array element into string with ',' as the separator

Tags:

php

Is there a quick way ( existing method) Concatenate array element into string with ',' as the separator? Specifically I am looking for a single line of method replacing the following routine:

//given ('a','b','c'), it will return 'a,b,c'
private static function ConstructArrayConcantenate($groupViewID)
{
    $groupIDStr='';
    foreach ($groupViewID as $key=>$value) {
        $groupIDStr=$groupIDStr.$value;
        if($key!=count($groupViewID)-1)
            $groupIDStr=$groupIDStr.',';
    }       

    return $groupIDStr;
}
like image 872
Graviton Avatar asked Sep 07 '09 00:09

Graviton


1 Answers

This is exactly what the PHP implode() function is for.

Try

$groupIDStr = implode(',', $groupViewID);
like image 77
Artelius Avatar answered Nov 05 '22 00:11

Artelius