Skip to content
Advertisement

How to print rows of CSV file with PHP?

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] => newton@ldap.forumsys.com [2] => [3] => newton ) Array ( [0] => Albert Einstein [1] => einstein@ldap.forumsys.com [2] => 314-159-2653 [3] => einstein ) Array ( [0] => Nikola Tesla [1] => tesla@ldap.forumsys.com [2] => [3] => tesla ) Array ( [0] => Galileo Galilei [1] => galieleo@ldap.forumsys.com [2] => [3] => galieleo ) Array ( [0] => Leonhard Euler [1] => euler@ldap.forumsys.com [2] => [3] => euler ) Array ( [0] => Carl Friedrich Gauss [1] => gauss@ldap.forumsys.com [2] => [3] => gauss ) Array ( [0] => Bernhard Riemann [1] => riemann@ldap.forumsys.com [2] => [3] => riemann ) Array ( [0] => Euclid [1] => euclid@ldap.forumsys.com [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] => curie@ldap.forumsys.com [2] => [3] => curie ) Array ( [0] => Alfred Nobel [1] => nobel@ldap.forumsys.com [2] => [3] => nobel ) Array ( [0] => Robert Boyle [1] => boyle@ldap.forumsys.com [2] => 999-867-5309 [3] => boyle ) Array ( [0] => Louis Pasteur [1] => pasteur@ldap.forumsys.com [2] => 602-214-4978 [3] => pasteur ) Array ( [0] => No Group [1] => nogroup@ldap.forumsys.com [2] => [3] => nogroup ) Array ( [0] => FS Training [1] => training@forumsys.com [2] => 888-111-2222 [3] => training ) Array ( [0] => FS Training [1] => jmacy-training@forumsys.com [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",newton@ldap.forumsys.com,,newton
"Albert Einstein",einstein@ldap.forumsys.com,314-159-2653,einstein
"Nikola Tesla",tesla@ldap.forumsys.com,,tesla
"Galileo Galilei",galieleo@ldap.forumsys.com,,galieleo
"Leonhard Euler",euler@ldap.forumsys.com,,euler
"Carl Friedrich Gauss",gauss@ldap.forumsys.com,,gauss
"Bernhard Riemann",riemann@ldap.forumsys.com,,riemann
Euclid,euclid@ldap.forumsys.com,,euclid
Mathematicians,,,
Scientists,,,
read-only-admin,,,
Italians,,,
Test,,,test
Chemists,,,
"Marie Curie",curie@ldap.forumsys.com,,curie
"Alfred Nobel",nobel@ldap.forumsys.com,,nobel
"Robert Boyle",boyle@ldap.forumsys.com,999-867-5309,boyle
"Louis Pasteur",pasteur@ldap.forumsys.com,602-214-4978,pasteur
"No Group",nogroup@ldap.forumsys.com,,nogroup
"FS Training",training@forumsys.com,888-111-2222,training
"FS Training",jmacy-training@forumsys.com,888-111-2222,jmacy

Advertisement

Answer

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","newton@ldap.forumsys.com",,"newton"
"Albert Einstein","einstein@ldap.forumsys.com","314-159-2653","einstein"
"Nikola Tesla","tesla@ldap.forumsys.com",,"tesla"
"Galileo Galilei","galieleo@ldap.forumsys.com",,"galieleo"
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement