php - multiple JSON files that I would like to parse, edit, and merge into one object, -


hi in stackexhange

i needed multiple .json file decode folder

i have multiple json files parse, edit, , merge 1 object, re-encoded , output single json.

her code of 1 .json file decode

<?php      if ( file_exists( base . '/contents/cache/recent-file.json' ) ) {         $recent_file = json_decode( file_get_contents( base . '/contents/cache/recent-file.json' ), true );         if ( $recent_file ) { ?>      <div class="items-list">             <?php                  foreach( array_reverse($recent_file) $key => $apps ) {                     get_template( 'templates/app-item', $apps );                                   ?>         </div>       <?php } } ?>   

you need to:

  1. get list of json files
  2. get contents of each file
  3. add contents of files php array
  4. output new json object

my method be:

//get json files $files = glob("/path/to/folder/*.json");  //create empty new array $newdataarray = [];  //get contents of each file foreach($files $file){     $thisdata = file_get_contents($file);      //decode json     $thisdataarray = json_decode($thisdata);      //add $thisdata new array     $newdataarray[] = $thisdataarray; }  //encode array json $newdatajson = json_encode($newdataarray); 

now can wish $newdatajson object, example save new .json file:

file_put_contents("/path/to/file.json",$newdatajson); 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -