php - Update some json items in existing file -
i'm trying simple script manipulate json, decode json file , create foreach make ifs looking information live games, live.json file:
{ match: [ { id: "348324", date: "2015-07-19t21:30:00+00:00", league: "brasileirao", round: "14", hometeam: "joinville", hometeam_id: "1208", homegoals: "1", awayteam: "ponte preta", awayteam_id: "745", awaygoals: "1", time: "67", location: "arena joinville", hometeamyellowcarddetails: { }, awayteamyellowcarddetails: { }, hometeamredcarddetails: { }, awayteamredcarddetails: { } }, { id: "348319", date: "2015-07-19t21:30:00+00:00", league: "brasileirao", round: "14", hometeam: "cruzeiro", hometeam_id: "749", homegoals: "1", awayteam: "avai fc", awayteam_id: "1203", awaygoals: "1", time: "finished", location: "mineirão", hometeamyellowcarddetails: { }, awayteamyellowcarddetails: { }, hometeamredcarddetails: { }, awayteamredcarddetails: { } }
check comments in code specify want in each part.
<?php $json_url = "http://domain.com/live.json"; // file comes live games $json = file_get_contents($json_url); $links = json_decode($json, true); foreach($links["match"] $key=>$val) { if($val['id'] == "live games id") // want live.json games ids match games same id in file.json // want update items below, , { $links["match"][$key]['time'] = ['time']; // in end want item time update file.json in item time game same id $links["match"][$key]['homegoals'] = ['homegoals']; // similar want time $links["match"][$key]['awaygoals'] = ['awaygoals']; } } $fp = fopen( "file.json","w+"); // file have list of games including ones came live.json, structure same. fwrite($fp,json_encode($links)); fclose($fp); ?>
the content of new file.json
{ match: [ { id: "348324", date: "2015-07-19t21:30:00+00:00", league: "brasileirao", round: "14", hometeam: "joinville", hometeam_id: "1208", homegoals: "1", awayteam: "ponte preta", awayteam_id: "745", awaygoals: "1", time: "69", location: "arena joinville", hometeamyellowcarddetails: { }, awayteamyellowcarddetails: { }, hometeamredcarddetails: { }, awayteamredcarddetails: { } }, { id: "348319", date: "2015-07-19t21:30:00+00:00", league: "brasileirao", round: "14", hometeam: "cruzeiro", hometeam_id: "749", homegoals: "1", awayteam: "avai fc", awayteam_id: "1203", awaygoals: "1", time: "finished", location: "mineirão", hometeamyellowcarddetails: { }, awayteamyellowcarddetails: { }, hometeamredcarddetails: { }, awayteamredcarddetails: { } }
somebody can ? best approach ?
Comments
Post a Comment