fgetcsv is skipping lines

784 views Asked by At

I have google contact file in CSV which I exported from google contacts - it's format has dividing each line with delimiter like this

First Name,Middle Name,Last Name,Title,Suffix,Initials,Web Page,Gender,Birthday,Anniversary,Location,Language,Internet Free Busy,Notes,E-mail Address,E-mail 2 Address,E-mail 3 Address,Primary Phone,Home Phone,Home Phone 2,Mobile Phone,Pager,Home Fax,Home Address,Home Street,Home Street 2,Home Street 3,Home Address PO Box,Home City,Home State,Home Postal Code,Home Country,Spouse,Children,Manager's Name,Assistant's Name,Referred By,Company Main Phone,Business Phone,Business Phone 2,Business Fax,Assistant's Phone,Company,Job Title,Department,Office Location,Organizational ID Number,Profession,Account,Business Address,Business Street,Business Street 2,Business Street 3,Business Address PO Box,Business City,Business State,Business Postal Code,Business Country,Other Phone,Other Fax,Other Address,Other Street,Other Street 2,Other Street 3,Other Address PO Box,Other City,Other State,Other Postal Code,Other Country,Callback,Car Phone,ISDN,Radio Phone,TTY/TDD Phone,Telex,User 1,User 2,User 3,User 4,Keywords,Mileage,Hobby,Billing Information,Directory Server,Sensitivity,Priority,Private,Categories
Example First Name,,Example Last Name,,,,,,,,,,,,[email protected],,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,My Contacts,
Example Name again,,Example Last Name again,,,,,,,,,,,,[email protected],,,,,,7623762763,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,My Contacts,

I am trying to parse it into array but the problem is, it's not giving me all the values when I use if statement to check if the given array count is greater than 0 using the count() function - it just gives me second array and skips the first and second

$file = fopen($path, 'r');
        $rows = [];
        $x = 0;
        while(! feof($file))
          {

            if (count(fgetcsv($file))) {
                // if array is empty we don't pass it to further proceeding
                echo "<pre>";
                print_r(fgetcsv($file));
            }
            echo $x;
            $x++;
          }

Why is it skipping first and second?

1

There are 1 answers

0
AnkiiG On

Just comment out the if() and it will print all the elements.

If file is empty then it will only echo 0.

Try below code :

$file = fopen($path, 'r');
$rows = [];
$x = 0;
while(! feof($file))
{
     //if (count(fgetcsv($file))) {
         // if array is empty we don't pass it to further proceeding
         echo "<pre>";
         print_r(fgetcsv($file));
     //}

   echo $x;
   $x++;
}