我有一個創建陣列的功能,然後我想要添加到主主陣,我可以再json_encode ...PHP陣列添加到陣列
因此,代碼
$pathtocsvs = "/var/www/csvfiless/";
$mainarray= array();
$filearray = getDirectoryList($pathtocsvs);
sort($filearray);
foreach ($filearray as $v) {
parseCSV($pathtocsvs. $v);;
}
print_r(json_encode($mainarray)); //outputs nothing but an empty [] json string
而parseCSV函數,我已經刪除了一些不重要的代碼。
function parseCSV($file){
$file_handle = fopen($file, "r");
$output = "";
$locations = array();
while (!feof($file_handle)) {
$line_of_text = fgetcsv($file_handle, 1024);
$lat = $line_of_text[0];
$lon = $line_of_text[1];
$output = $lat.",". $lon ;
array_push($locations,$output);
}
array_push($mainarray,$locations); //line 47 that is the error
print_r($locations); //This does display the array
print_r($mainarray); //This displays nothing
fclose($file_handle);
}
,並出現在日誌中這個錯誤...
array_push() expects parameter 1 to be array, null given in /var/www/test.php on line 47
哪條線是第47條? – 2011-05-23 08:29:51
array_push($ mainarray,$ locations); – 2011-05-23 08:32:52