简体   繁体   English

使用PHP从文件中读取大型js数组

[英]read large js array from a file using PHP

I'm trying to process a large(150MB+) JavaScript array from a CSV file. 我正在尝试从CSV文件处理大型(150MB +)JavaScript数组。

Here is what the array looks like: 这是数组的样子:

var results = [
{"requestId":"1","responseId":"1","time":"11","opacity":"0.6"}
,
{"requestId":"1","responseId":"2","time":"12","opacity":"0.7"}
,
{"requestId":"1","responseId":"3","time":"13","opacity":"0.8"}
,
{"requestId":"1","responseId":"4","time":"14","opacity":"0.9"}
,
{"requestId":"1","responseId":"5","time":"15","opacity":"0.1"}
....
];

There are about 100,000 lines. 大约有100,000行。 I need to take each of the values from each of the array elements. 我需要从每个数组元素中获取每个值。

For instance, I need to take 1, 1, 11 and 0.6 from "requestId", "responseId", "time" and "opacity" respectively. 例如,我需要分别从“requestId”,“responseId”,“time”和“opacity”中取1,1,11和0.6。

I've tried to use *file_get_contents* (failed, couldn't read the file, it is too big), and file stream, also the following code to read, but i don't know how to extract the numbers using PHP. 我试过使用* file_get_contents *(失败,无法读取文件,它太大了)和文件流,还要读取以下代码,但我不知道如何使用PHP提取数字。 Thanks so much for your help. 非常感谢你的帮助。 Is there anyway that I can convert this into PHP array or JSON? 无论如何我可以将其转换为PHP数组或JSON吗? I like to process the numbers as quick as possible though... 我喜欢尽快处理这些数字......

$fh = fopen($cachedFile, 'r');
$Data = fread($fh, filesize($cachedFile));
fclose($fh);
echo "<pre>";
echo $Data;
echo "</pre>";

failed, couldn't read the file, it is too big 失败了,无法读取文件,实在是太大了

This is a problem with your PHP config file limit. 这是PHP配置文件限制的问题。 You can change it within php.ini to: 您可以在php.ini中将其更改为:

memory_limit = 200M

Or add the following to your PHP script: 或者将以下内容添加到PHP脚本中:

ini_set('memory_limit', '200M');

I don't know how to extract the numbers using PHP 我不知道如何使用PHP提取数字

In order to get values, you can use something like this: 为了获得值,您可以使用以下内容:

$fh = fopen('a.txt', 'r');
$Data = fscanf($fh, '{"requestId":"%d","responseId":"%d","time":"%d","opacity":"%f"}');

print_r($Data);

This will output something like this: 这将输出如下内容:

Array
(
    [0] => 1
    [1] => 1
    [2] => 11
    [3] => 0.6
)

Note that in the above code, I assumed that your file have multiple lines of '{"requestId":"%d","responseId":"%d","time":"%d","opacity":"%f"}' . 请注意,在上面的代码中,我假设您的文件有多行'{"requestId":"%d","responseId":"%d","time":"%d","opacity":"%f"}' You can change it to whatever you want 您可以将其更改为您想要的任何内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM