我是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,以存储此数组的值。
任何帮助都会很棒!
答案 0 :(得分:2)
你以错误的方式使用fgetcsv()
。
我们在chatting here on 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];
}