Reading and storing partial data from a file
I have a question about reading chunks of data from a file.
Suppose I have the following array saved in a file:
Is there anyway I can open the file but just read and store the "Chicago" array elements in a variable? The reason I am asking is because The file many contain a huge list of arrays and reading the entire array and dumping them in the memory wont be a good idea. So how can we accomplish this?
[Japan] => Array
 => 101
 => 102
 => 103
[China] => Array
 => 202
 => 203
 => 204
[Chicago] => Array
 => 303
 => 304
 => 305
Thanks in advance
PS: The extension of the file is .json and the data storage format would be JSON as well..
Last edited by phantom007; 03-09-2014 at 03:09 AM.
You're going to have to read the entire file, then search, however, PHP has some very nice built-in functions that make this very easy. For example, try something like:
$jsonFile = json_decode(file_get_contents($myJsonFile), true);
The second parameter of true will convert the result into an associative array
Thanks Error404 for your reply, however I do not want to do that. Imagine a file 1 GB in size, wouldn't dumping the entire content into the memory will slow down the system?
Basically I need to implement an DBMS type functionality into my project where I just retrieve the required data from a file or update the relevant data not the entire file.
It would tax the system if you're repeatedly opening and accessing data from that JSON file, in which case, why not just use a DBMS? If the JSON format would always be as how you indicated in your example, then each country would have its own column. If there's already a lot of data in your JSON file, then you can either use something like MongoDB, in which case it should read your data as is without any conversion or processing on your part but if you want to use something like MySQL, you're going to have to write a short script or download an already made, out-of-the-box solution.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)