user007 user007 - 1 month ago 8
PHP Question

PHP - remove comma from the last loop

I have a PHP while LOOP, and I want to remove last comma

,
from
echo '],';
if it is last loop

while($ltr = mysql_fetch_array($lt)){
echo '[';
echo $ltr['days']. ' ,'. $ltr['name'];
echo '],';
}

Answer
$str = '';
while($ltr = mysql_fetch_array($lt)){
    $str .= '[';
    $str .= $ltr['days']. ' ,'. $ltr['name'];
    $str .= '],';
}

echo rtrim($str, ",");

this will remove the last , from string