だから、私はあなたのためにそれを書くことを本当に試みるべきではありませんが、私は今朝本当に良い気分です。これを試して:
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;
}
}
その後、次のように使用できます。
$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();