【发布时间】:2016-01-13 20:42:24
【问题描述】:
我一直在研究 fgetcsv() 并且有以下工作代码:
$file = fopen($pathToCsv,"r");
$data = array();
while(! feof($file))
{
array_push($data, fgetcsv($file));
}
fclose($file);
但是,当我尝试对其进行调整以动态接受存储到数组中的未知数量的 csv 文件时,事情就停止了:
$year = $_POST['year'];
$m = "maths".$year;
$sheets = $$m; //an array containing between 5 an 8 paths to csv and other info
$data = array();
function ArrayFromCsv($file, $i) {
while(! feof($file))
{
array_push($data[$i], fgetcsv($file)); //line 15
}
fclose($file);
}
for ($i = 0; $i < count($$m); $i++){
array_push($data, array());
$x = fopen($sheets[$i]['csv'],'r');
ArrayFromCsv($x, $i);
}
我得到:Warning: array_push() expects parameter 1 to be array, null given in ... on line 15
我不知道如何进一步研究。有没有更好的方法或明显的错误?我尝试了很多方法。
【问题讨论】:
-
$data在您的ArrayFromCsv函数范围内未定义。 -
php.net/manual/en/language.variables.scope.php 可以帮助您进一步研究。