【发布时间】:2013-03-14 06:25:09
【问题描述】:
我正在尝试读取一个 csv 文件,然后将第一列和第 21 列存储在关联数组中,以便第一列成为键,第 21 列成为值。
稍后我想根据“键”提取记录。包含代码的PHP文件是upload.php
$calls = array();
$file_handle = fopen($C1.".File.csv","r"); // $C1 is defined before.
//Just appending something to the file name. This file exists.
while (!feof($file_handle) ) {
$line= fgetcsv($file_handle, 1024);
$calls[$line[0]] = $line[20]; //Line 94 of this file
}
fclose($file_handle);
print_r($calls);
我收到此错误
Undefined offset: 20 in upload.php on line 94
我哪里错了。
【问题讨论】:
-
第 94 行的 upload.php 中有什么内容?因为您在这里的任何地方都没有使用偏移量 24,所以您的问题一定在其他地方。
-
您在该行中缺少逗号。或者有可能整行都是空白的。 var_dump($line);
-
对不起,在这种情况下偏移 20.. 不是 24
-
考虑使用
fgetcsv()功能手册:php.net/manual/en/function.fgetcsv.php
标签: php arrays associative-array