Php download large json file once

I created a local web app to display and interact with data that received a large Json file (about 250 Mo). I have several functions to display data differently, but currently each one starts by reading and parsing a Json file:

$string = file_get_contents("myFile.json");
$json_a = json_decode($string, true);

      

The thing is, it's pretty slow (about 4 seconds) because the file is large ... So I would like to parse the Json file once and for all and store the parsed data in memory so that every function can use this:

session_start();
$string = file_get_contents("myFile.json");
$_SESSION['json_a'] = json_decode($string, true);

      

and then use $_SESSION['json_a']

in my functions. But I got an error when the function accessing this variable $_SESSION['json_a']

:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 195799278 bytes)

      

I'm guessing it's because the file is too big ... but why does it crash when the variable is used and not when it is built? And why can I do this with my first solution (parsing every time) if it's too big for memory?

And finally my real question is, how can I optimize this? (I know sql database will be much better, but I have json data)

+3


source to share





All Articles