www.webdeveloper.com
Results 1 to 4 of 4

Thread: Reading and storing partial data from a file

  1. #1
    Join Date
    Sep 2006
    Posts
    643

    Question Reading and storing partial data from a file

    Hello

    I have a question about reading chunks of data from a file.

    Suppose I have the following array saved in a file:

    Code:
    Array
    (
        [Japan] => Array
            (
                [0] => 101
                [1] => 102
                [2] => 103
            )
    
        [China] => Array
            (
                [0] => 202
                [1] => 203
                [2] => 204
            )
    
        [Chicago] => Array
            (
                [0] => 303
                [1] => 304
                [2] => 305
            )
    
    )
    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?


    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.

  2. #2
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    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:

    PHP Code:
    <?php
         $jsonFile 
    json_decode(file_get_contents($myJsonFile), true);
    ?>

    The second parameter of true will convert the result into an associative array

  3. #3
    Join Date
    Sep 2006
    Posts
    643
    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.

  4. #4
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles