0

我是 php 编程的新手,但我已经被这段代码困住了一段时间。我想逐行读取 .csv 文件,然后将其值保存在数组列表中。

$file = fopen('Sub-Companies.csv', 'r');    
while (($line =
fgetcsv($file)) !== FALSE) { 
print_r($line);
list($customer_id[],$company_name[],$department[],$employee[],$country[],$zipcode[],$address[],$city[],
$smth1[], $smth2[], $phone_no1[],$phone_no2[],$email[],$website[],
$customer_no[],$problem1[],$problem2[]) = explode(";",$line);  }
fclose($file); var_dump($customer_id);

问题是,尽管正确读取了文件,但爆炸不起作用并且数组似乎为空。

我正在考虑的一件事是一些数组有更多的“;” 比其他人,所以这可能是一个问题,这就是为什么我有数组 $problem1 和 $problem2,以便存储这个数组的值。

任何帮助都会很棒!

4

1 回答 1

2

你用fgetcsv()错了方法。

我们在 StackOverflow 上聊天时来到了这个解决方案。

<?php
// Create file data.csv with your data
$handle = fopen('Sub-Companies.csv', 'r');

$customer_id = array();
$xyz_array = array();
// ...

// Better use a specified length (second parameter) instead of 0
// It slows down the whole process of reading the data!
while (($line = fgetcsv($handle, 0, ';')) !== FALSE) {
  $customer_id[] = $line[0];
    $xyz_array[] = $line[1];
}
于 2012-11-15T21:22:03.077 回答