0

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>";
3
  • Why not consider a database that can import json like MongoDB Commented Mar 29, 2013 at 19:16
  • can i import the js array into mysql? Commented Mar 29, 2013 at 19:49
  • Yes you can import json to mysql but not by default see karlssonondatabases.blogspot.se/2012/07/… Commented Mar 29, 2013 at 19:56

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, i haven't tried this yet, but what I really want to know is how I can get the numbers from the array using PHP....
it did not return anything..except that it returned ( [0] => [1] => [2] => [3] => )
As I mentioned in the last line, I assumed that your file have multiple lines of {"requestId":"%d","responseId":"%d","time":"%d","opacity":"%f"}. How does your file really look like ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.