39

次の csv ファイルがあります。

upc/ean/isbn,item name,category,supplier id,cost price,unit price,tax 1 name,tax 1 percent,tax 2 name,tax 2 percent,quantity,reorder level,description,allow alt. description,item has serial number
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,
,Apple iMac,Computers,,10,12,8,8,10,10,10,1,Best computer,,

私がこれを行うとき:

if (($handle = fopen($_FILES['file_path']['tmp_name'], "r")) !== FALSE) 
{
    $data = fgetcsv($handle);
    var_dump($data);
}

183 要素の配列を取得します。取得できるのは csv の 1 行のみですが、ファイル全体を取得します。

私も試し$data = fgetcsv($handle, 1000);てみましたが、同じ結果が得られました

4

6 に答える 6

107

これはおそらく行末の問題です。auto_detect_line_endingsファイルの行末を決定しようとする which を有効にしてみてください。

ini_set('auto_detect_line_endings', true);

それでも問題が解決しない場合は、次のコマンドを使用して行末記号のタイプを検出します。file

$ file example.csv
example.csv: ASCII text, with CR line terminators

その後、行末を変換できます。お使いの OS はわかりませんが、ファイル形式を変換するためのユーティリティはたくさんありますdos2unix

于 2011-04-04T16:18:28.880 に答える
7

これは私が考えることができる最短の解決策です:

$fp = fopen('test.csv', 'r');

// get the first (header) line
$header = fgetcsv($fp);

// get the rest of the rows
$data = array();
while ($row = fgetcsv($fp)) {
  $arr = array();
  foreach ($header as $i => $col)
    $arr[$col] = $row[$i];
  $data[] = $arr;
}

print_r($data);

出力:

Array
(
    [0] => Array
        (
            [upc/ean/isbn] => 
            [item name] => Apple iMac
            [category] => Computers
            [supplier id] => 
            [cost price] => 10
            [unit price] => 12
            [tax 1 name] => 8
            [tax 1 percent] => 8
            [tax 2 name] => 10
            [tax 2 percent] => 10
            [quantity] => 10
            [reorder level] => 1
            [description] => Best computer
            [allow alt. description] => 
            [item has serial number] => 
        )

    [1] => Array
        (
            [upc/ean/isbn] => 
            [item name] => Apple iMac
            [category] => Computers
            [supplier id] => 
            [cost price] => 10
            [unit price] => 12
            [tax 1 name] => 8
            [tax 1 percent] => 8
            [tax 2 name] => 10
            [tax 2 percent] => 10
            [quantity] => 10
            [reorder level] => 1
            [description] => Best computer
            [allow alt. description] => 
            [item has serial number] => 
        )

    // ...

    [11] => Array
        (
            [upc/ean/isbn] => 
            [item name] => Apple iMac
            [category] => Computers
            [supplier id] => 
            [cost price] => 10
            [unit price] => 12
            [tax 1 name] => 8
            [tax 1 percent] => 8
            [tax 2 name] => 10
            [tax 2 percent] => 10
            [quantity] => 10
            [reorder level] => 1
            [description] => Best computer
            [allow alt. description] => 
            [item has serial number] => 
        )

)
于 2011-04-04T14:39:57.310 に答える
1

CSVファイル全体を一度に読み込むには、次を使用します。

$data = array_map("str_getcsv", file($fn));
var_dump($data);

ただし、最初の行をarray_shift()する必要があります(列キーを使用しない場合)。


不特定の行末の回避策:

$data = array_map("str_getcsv", preg_split('/[\r\n]+/', file_get_contents($fn)));
var_dump($data);
于 2011-04-04T13:34:16.900 に答える
1

試す:

 while (($data = fgetcsv($handle, 0, ',')) !== FALSE) {
    $columns = count($data);
 }

または、使用してみることができますstr_getcsv(file_get_contents(...))

于 2011-04-04T13:22:14.483 に答える
0

オプションがある場合は、戻って .csv ファイルを編集し、「Unix」スタイルの行末を含めます...その後、fgetcsv は配列を行末で自動的に分割します

于 2012-08-29T01:20:30.770 に答える