简体   繁体   中英

Load large amount of json data in php

I'm a beginner with this so sorry for the silly question,

I have a json with a large amount of data in it and I have to process this file in php. (Edit/Delete/Filter Objects).

How I am thinking to do it: a class that contains a list of objects and the functions to load objects from json and another to write them back. Also, other functions for operations over the list.

What I'm asking is what is the best way to do this, because my idea is the simplest one, than each time I click on : Edit, Delete, Filter I have to reload again and again the loatJsonObjects(), can you give me a better way to do this, that won't make so many operations each time? Thank you !

So, I shouldn't really attempt to write one for you, but I'm in a really good mood this morning. Try this:

Class Json_Handler {
  private $json_object;
  public function __construct($json_data) {

    if(is_object($json_data) {
      $this->json_object = $json_data;
    }

  }

  public function edit($key, $value) {

    if(isset($this->json_data[$key])) {
     $this->json_data[$key] = $value;
     return $this;
    }
    else {
     return FALSE;
    }

  }

  public function delete($key) {
    if(isset($this->json_data[$key])) {
      unset($this->json_data[$key]);
      return $this;
    }
    else {
      return FALSE;
    }
  }

  public function filter($on = 'key', $filter == NULL) {
     $filtered_objects = array();
     switch($on) {
       case 'key':
       default:
        foreach($this->json_data as $key => $value) {
          if($key == $value) {
            $filtered_objects[] = array($key => $value);
          }
        }
        break;
        case 'value':
         foreach($this->json_data as $key => $value) {
           $filtered_objects[] = array($key => $value);
         }
        break;
     }
     return $filtered_objects;
  }

  public function get_json() {
    return json_encode($this->json_data);
  }

  public function set_json($json) {
    $this->json_data = (is_object($json) ? $json : json_encode($json));
    return $this;
  }

}

You can then use it as so:

$my_json_data = json_decode([...]);
$json = new Json_Handler($my_json_data);
echo $json->edit('test', 'now i have this value')
          ->delete('test')
          ->filter('key', 'bar');
          ->set_json(json_encode($new_json)
          ->get_json();

You can decode the JSON data to Array and store it in Global Variable or Session variable. Then Uses that session variable for Edit, Delete, Filter operations. I dont know this is good or not but this is one of the way to do.

Let Me know any other ways....

If you can keep the data in a plain PHP data object and have your "processing" routines within this object, then you could reduce the amount of operations you'd need to re-perform on that data.

This object could then be cached, speeding up future data retrieval. Also, you'll not need to re-run these expensive operations on the same dataset (until the cached item expires).

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