2015-06-16 87 views
0

我有CSV谷歌聯繫人文件這是我從谷歌聯繫人導出 - 這是格式已經將每一行分隔符這樣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,,,,,,,,,,,,[email protected],,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,My Contacts, 
Example Name again,,Example Last Name again,,,,,,,,,,,,[email protected],,,,,,7623762763,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,My Contacts, 

我想它解析爲數組,但問題是,這不是給我所有的值當我使用if語句來檢查,如果給定的數組數大於0使用count()功能 - 它只是給了我第二陣列,並跳過第一和第二

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

爲什麼它跳過f第一次和第二次?

回答

0

只需將if()註釋掉,它將打印所有元素。

如果文件是空的,那麼只有呼應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++; 
}