我正在从365 csv文件加载数据到hilitock图形api。我使用PHP来读取csv文件并创建数组来保存信息。然而,我遇到了:
致命错误:允许的内存大小为67108864字节耗尽
如何解决这个问题?
希望创建这个图表:
http://www.highcharts.com/stock/demo/compare
与其将内存中的所有内容表示为数组,不如直接使用json文件。我将假设您需要的数据是一个包含时间戳+ 6个浮点字段的二维多维数组。
在不了解如何将信息提供给图表api的很多细节的情况下,这里是第一个尝试。
$tmpFile = tempnam("tmp/","highchart_");
$out = fopen($tmpFile, "w");
// we are not going to use json encode because it requires us to hold the array in memory.
fputs($out, "[");
for($i=0;$i<count($files);$i++){
if (($handle = fopen($files[$i], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// You may be able to get arround the timestamp calculation by just saying
$timestamp = strtotime($data[0]." ".$data[1]);
fputs($out, "[".(int)$timestamp.",".(float)$data[2].",".
(float)$data[3].",".(float)$data[4].",".(float)$data[5].",".(float)$data[13]."]");
}
fclose($handle);
}
}
fputs($out, "]");
fclose($out);
现在$tmpFile
的文件将包含一个json编码的数组,然后您可以使用readfile或fpassthru来访问浏览器。另外,我强烈建议您使用某种缓存机制,而不是仅仅将它们存储在临时文件中。+67MB的数据对于处理每个请求来说是相当庞大的。