2

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 '],';
            }
1
  • 1
    do not use mysql_*. try PDO:: Commented Feb 5, 2013 at 6:20

9 Answers 9

7

Create an array with the elements as you go along so that they look like array = ([ELEMENT INFO], [ELEMENT INFO], [ELEMENT INFO]) and then implode the array with a comma.

Sign up to request clarification or add additional context in comments.

1 Comment

This method produces the nicest code and I don't have any reason to think it will be less efficient than any other, less elegant methods, with the possible exception of if there are a huge number of elements.
6
$str = '';
while($ltr = mysql_fetch_array($lt)){
    $str .= '[';
    $str .= $ltr['days']. ' ,'. $ltr['name'];
    $str .= '],';
}

echo rtrim($str, ",");

this will remove the last , from string

Comments

4

I think the systemic solution is following:

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

No call for count(), no additional iteration of implode(), no additional string operations, ready for any (unpredictable) number of results.

Comments

3
$result = mysql_fetch_array($lt);
for ($i=0;$i<=(count($result)-1);$i++) {
    $ltr = $result[$i];
    echo '[';
    echo $ltr['days']. ' ,'. $ltr['name'];
    echo ']';
    if(!count($result)-1 == $i){
        echo ',';
    }
}

1 Comment

I like this answer instead of using rtrim as you don't have to maintain a string the size of which depends on loop.
1

Check how many entries you have, make a "Counter" and a condition to only put the comma when its not the last loop.

Comments

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

echo implode(',', $arr);

Comments

1
$res_array = array();

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

$str = implode(",",$res_array);
echo $str; 

1 Comment

mysql_* is deprecated, use mysqli OR PDO
1

Save the response as a var instead of echoing it and then remove the final character at the end using substr.

      $response = "";
       while($ltr = mysql_fetch_array($lt)){
            $response .= '[';
            $response .= $ltr['days']. ' ,'. $ltr['name'];
            $response .= '],';
        }
      echo substr($response, 0, -1);

Comments

0
//this one works
$result = mysql_fetch_array($lt);
for ($i=0;$i<=(count($result)-1);$i++) {
    $ltr = $result[$i];
    echo '[';
    echo $ltr['days']. ' ,'. $ltr['name'];
    echo ']';
    if(count($result)-1 != $i){
        echo ',';
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.