Help with this code does not remove the comma to the last element in this cycle

1
$i = 0;
while ($i < $cuenta) {

        $actualizaInfoActualizar.= '$'.$campos[$i].', ';
        $i++;       
}

I want to remove the (,) that is generated in the last element, the others are fine.

    
asked by Tomas Taveras 30.05.2018 в 06:48
source

3 answers

4
$i = 0;
while ($i < $cuenta) {

        $actualizaInfoActualizar.= '$'.$campos[$i].', ';
        $i++;       
}

$actualizaInfoActualizar = rtrim($actualizaInfoActualizar,",");

There he is compadre.

    
answered by 30.05.2018 в 07:17
2

You can control when the last element is written without a comma.

$i = 0;

    while ($i < $cuenta) {
            if($i == ($cuenta-1)){
                $actualizaInfoActualizar.= '$'.$campos[$i];   
            }else{
                $actualizaInfoActualizar.= '$'.$campos[$i].', ';
            }
            $i++;       
    }
    
answered by 30.05.2018 в 08:12
2

The answer, make a substr() , once the loop is finished:

 $actualizaInfoActualizar = substr($actualizaInfoActualizar,0,-1);

And it would be already. I hope it serves you:)

    
answered by 30.05.2018 в 08:22