fgetcsv正在跳过线路

时间:2015-06-16 05:40:13

标签: php fgetcsv

我有谷歌的谷歌联系人文件,我从谷歌联系人导出 - 它的格式已经用这样的分隔符划分每一行

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,,,,,,,,,,,,example1@gmail.com,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,My Contacts,
Example Name again,,Example Last Name again,,,,,,,,,,,,example2@gmail.com,,,,,,7623762763,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,My Contacts,

我正在尝试将其解析为数组,但问题是,当我使用if语句使用count()检查给定的数组计数是否大于0时,它并没有给我所有的值功能 - 它只给我第二个数组并跳过第一个和第二个

$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++;
          }

为什么跳过第一个和第二个?

1 个答案:

答案 0 :(得分:0)

只需注释掉if(),它就会打印出所有元素。

如果file为空,那么它只会回显0。

尝试以下代码:

$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++;
}