Remove the last comma from an array of a While

Asked

Viewed 399 times

1

I’m trying to remove the last comma from an Array but I’m not getting it.

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

Follows 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


    }
?>
  • It would not replace($number_Aut,0,-3); ?

  • ($number_Aut,0,-2) takes out all the commas O ($number_Aut,0,-3) takes the ',

  • if you have the example of this file ?

  • question beast, but its CSV is bounded by , or ;?

  • something else, here Oce adds the comma $number_Aut = "'$data[13]','"; 2 lines after Voce tries to remove the last comma, which just added. It’s not very clear, if you can explain better what you want to do it makes it much easier.

  • My file is demilitated by ; In a certain line I remove ; and replace by ' ', to play in the Where of a query so far all right the difficulty is being in removing the last comma

Show 1 more comment

2 answers

3

If I understand correctly your problem is only to delete a comma at the end of the string if that’s all you try:

rtrim($number_Aut,',')

But by your code I believe you have to take out the ' too, because you’re adding comma and quotes:

rtrim(rtrim($number_Aut, "'"),",")
  • So far I do not know if it has solved the problem of the author of the question, but an upvoto for introducing me to something I have never seen before.

  • I liked the solution but I tried to use and did not work the first erases all the commas and the second erases the last quote and the commas

2


When you say in your code comment //I delete the last comma from the ARRAY BUT IT’S NOT WORKING in fact you are eliminating the last simple quotes.

$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:

arquivo satelite.csv

  • I did exactly as your example went right in parts he took out the last comma but started replicating the results. conforme o exemplo abaixo:&#xA;'1','1','1' '2','2','2' '5','5','5' '6','6','6' '7','7','7' '8','8','8' '9','9','9' '10','10','10'

  • @Shaolin Fantastic edited the question by asking a while and it worked!

  • I realized that the structure of your While was a little different from mine I followed exactly your example and it worked! Thank you very much!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.