How to make sure that the last element in the array does not display a comma, but a period is displayed?

while($arFields = $res->Fetch()){ echo $arFields["NAME"].", "; } 

    3 answers 3

    If the array is removed into the script's RAM, you can proceed as follows

     <?php $arr = array(); while($arFields = $res->Fetch()){ $arr[] = $arFields["NAME"]; } if(count($arr) > 0) { echo implode(", ", $arr)."."; } 

      print a comma before the next one, and after all add a period:

       $first = true; while($arFields = $res->Fetch()){ echo ($first ? '' : ', ') . $arFields["NAME"]; $first = false; } echo '.'; 
         $string = ""; while($arFields = $res->Fetch()){ $string .= $arFields["NAME"].", "; } echo rtrim($string, ", ")."."; 
        • 2
          better than rtrim() , or the first name will suffer, which (suddenly?) begins with "," - Sergiks