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:
- get list of json files
- get contents of each file
- add contents of files php array
- 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
Post a Comment