0

Here's the code that I'm using, but it doesn't output exactly what I want.

<?php
$file = fopen("ad.csv","r");

while(! feof($file))
  {
  print_r(fgetcsv($file));
  }

fclose($file);
?>

Here's what it outputs currently:

Array ( [0] => cn [1] => mail [2] => telephonenumber [3] => uid ) Array ( [0] => [1] => [2] => [3] => ) Array ( [0] => admin [1] => [2] => [3] => ) Array ( [0] => Isaac Newton [1] => [email protected] [2] => [3] => newton ) Array ( [0] => Albert Einstein [1] => [email protected] [2] => 314-159-2653 [3] => einstein ) Array ( [0] => Nikola Tesla [1] => [email protected] [2] => [3] => tesla ) Array ( [0] => Galileo Galilei [1] => [email protected] [2] => [3] => galieleo ) Array ( [0] => Leonhard Euler [1] => [email protected] [2] => [3] => euler ) Array ( [0] => Carl Friedrich Gauss [1] => [email protected] [2] => [3] => gauss ) Array ( [0] => Bernhard Riemann [1] => [email protected] [2] => [3] => riemann ) Array ( [0] => Euclid [1] => [email protected] [2] => [3] => euclid ) Array ( [0] => Mathematicians [1] => [2] => [3] => ) Array ( [0] => Scientists [1] => [2] => [3] => ) Array ( [0] => read-only-admin [1] => [2] => [3] => ) Array ( [0] => Italians [1] => [2] => [3] => ) Array ( [0] => Test [1] => [2] => [3] => test ) Array ( [0] => Chemists [1] => [2] => [3] => ) Array ( [0] => Marie Curie [1] => [email protected] [2] => [3] => curie ) Array ( [0] => Alfred Nobel [1] => [email protected] [2] => [3] => nobel ) Array ( [0] => Robert Boyle [1] => [email protected] [2] => 999-867-5309 [3] => boyle ) Array ( [0] => Louis Pasteur [1] => [email protected] [2] => 602-214-4978 [3] => pasteur ) Array ( [0] => No Group [1] => [email protected] [2] => [3] => nogroup ) Array ( [0] => FS Training [1] => [email protected] [2] => 888-111-2222 [3] => training ) Array ( [0] => FS Training [1] => [email protected] [2] => 888-111-2222 [3] => jmacy )

In other words, it's almost as if it's adding all my CSV data into a multidimensional array...

What I want is to just output it as I see it like so:

cn,mail,telephonenumber,uid
,,,
admin,,,
"Isaac Newton",[email protected],,newton
"Albert Einstein",[email protected],314-159-2653,einstein
"Nikola Tesla",[email protected],,tesla
"Galileo Galilei",[email protected],,galieleo
"Leonhard Euler",[email protected],,euler
"Carl Friedrich Gauss",[email protected],,gauss
"Bernhard Riemann",[email protected],,riemann
Euclid,[email protected],,euclid
Mathematicians,,,
Scientists,,,
read-only-admin,,,
Italians,,,
Test,,,test
Chemists,,,
"Marie Curie",[email protected],,curie
"Alfred Nobel",[email protected],,nobel
"Robert Boyle",[email protected],999-867-5309,boyle
"Louis Pasteur",[email protected],602-214-4978,pasteur
"No Group",[email protected],,nogroup
"FS Training",[email protected],888-111-2222,training
"FS Training",[email protected],888-111-2222,jmacy
tobeydw
  • 107
  • 1
  • 12
  • [why `while(!feof($file))` is wrong](https://stackoverflow.com/questions/34425804/php-while-loop-feof-isnt-outputting-showing-everything) – Barmar Jul 01 '20 at 22:30
  • The whole point of `fgetcsv()` is that it parses a CSV line into an array. If you don't want it to be an array, use `fgets()`. – Barmar Jul 01 '20 at 22:31
  • If you just want to print the whole CSV file, use `readfile("ad.csv");` You don't need a loop at all. – Barmar Jul 01 '20 at 22:32

1 Answers1

2

How to parse a CSV file using PHP has interesting suggestions

btw in php 7.2 this would work too:

    <?php
    $row = 1;
    if (($handle = fopen("ad.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            echo ($data[0].",".$data[1].",".$data[2].",".$data[3]."\n");
        }
        fclose($handle);
    }
    ?>

also, the quotes on the .csv data should enclose all data such as:

"Isaac Newton","[email protected]",,"newton"
"Albert Einstein","[email protected]","314-159-2653","einstein"
"Nikola Tesla","[email protected]",,"tesla"
"Galileo Galilei","[email protected]",,"galieleo"
Ruperto
  • 326
  • 2
  • 8