Reco Jhonatan
Reco Jhonatan

Reputation: 1623

Modify a json file with laravel

as I can modify a json in laravel file? In the methods available for the File class, only allows me to add additional text:

File::append($path, $data);

however, can not find a method to modify.

example:

public function json(){
      $data = json_encode([
         'data1' => 'hello',
          'data2' => 'world' ]);
  File::append($path, $data);
}

public function ModifyJson(){

//how change value for data2?  

}

thank you very much for the help :)

Upvotes: 2

Views: 3148

Answers (1)

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92557

Use $data = json_decode($your_file_content,true), then modify $data as normal php array, and before save use json_encode($data) and save data.

Upvotes: 3

Related Questions