简体   繁体   中英

read large js array from a file using PHP

I'm trying to process a large(150MB+) JavaScript array from a CSV file.

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. 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.

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. Thanks so much for your help. Is there anyway that I can convert this into PHP array or 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. You can change it within php.ini to:

memory_limit = 200M

Or add the following to your PHP script:

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

I don't know how to extract the numbers using 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"}' . You can change it to whatever you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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