Remove the last comma from an array of a While

1

I'm trying to remove the last comma from an Array but I can not.

I have a CSV file in which I read and retrieve an array with a code to be used in a Where of a sql Query. I am using substr ($ number_Aut, 0, -1) but it is not working.

Follow the code:

<?
while(! feof($CSVfp)) {
    $data = fgetcsv($CSVfp, 1000, ";");


    $number_Aut =  "'$data[13]','"; //Pego o código desejado no arquivo CSV e adiciono Aspas simples e virgula
    if ($data[13] != '') { //Verifico se entre as aspas não tem campos Vazios
    echo substr($number_Aut,0,-1); //Elimino a ultima virgula do ARRAY MAS NÃO ESTÁ FUNCIONANDO
}


?>
<tr>
<td align="center"><?php echo date('d/m/Y', strtotime($data[4])); ?></td>
<td align="center"><?php echo $data[11]; ?></td>
<td align="center"><?php echo $data[13]; ?></td>
<td align="center"><?php echo $data[2]; ?></td>
<td align="center"><?php echo $data[9]; ?>/<?php echo $data[10]; ?></td>
<td align="center"><?php echo number_format( $data[8], 2, ',', '.'); ?></td>
<td align="center"><?php echo number_format( $data[17], 2, ',', '.'); ?></td>
</tr>
<?php


    }
?>
    
asked by anonymous 19.06.2017 / 19:37

2 answers

2

When you say in the comment of your code // Delete the last comma of ARRAY BUT NOT WORKING you are actually deleting the last single quote.

$var="";
if (($handle = fopen("http://dominio.com/Satellite.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
        $num = count($data);
        for ($c=0; $c < $num; $c++) {
             $number_Aut =  "'$data[$c]','"; //Pego o código desejado no arquivo CSV e adiciono Aspas simples e virgula
             if ($data[$c] != '') { //Verifico se entre as aspas não tem campos Vazios
              $var.= substr($number_Aut,0,-1); //AQUI ESTÁ ELIMINANDO UMA ASPA SIMPLES
             }
        }
    }
    fclose($handle);
}

$var= substr($var,0,-1); //AQUI ELIMINA A ULTIMA VIRGULA
echo $var;

The Satellite.csv file is as shown in the figure below:

    
20.06.2017 / 04:53
4

If I understand correctly, your problem is only to delete a comma at the end of the string if it is just try:

rtrim($number_Aut,',')

But for your code, I think you have to get the '' too, because you're adding commas and quotation marks:

rtrim(rtrim($number_Aut, "'"),",")
    
20.06.2017 / 03:56