How can I easily remove the last comma from an array?

前端 未结 10 1108
予麋鹿
予麋鹿 2020-12-04 01:50

Let\'s say I have this:

$array = array(\"john\" => \"doe\", \"foe\" => \"bar\", \"oh\" => \"yeah\");

foreach($array as $i=>$k)
{
echo $i.\'-\'.$         


        
10条回答
  •  温柔的废话
    2020-12-04 02:45

    Alternatively you can use the rtrim function as:

    $result = '';
    foreach($array as $i=>$k) {
        $result .= $i.'-'.$k.',';
    }
    $result = rtrim($result,',');
    echo $result;
    

提交回复
热议问题