php - Json extra fields -


i created working code storing data api json file based on yesterday's date.

$filedate = date('y-m-d',strtotime("-1 days"));     $yesterday = date('y-m-d 00:00',strtotime("-1 days"));     $today = date('y-m-d 23:59',strtotime("-1 days"));      try{          $soccer=new xmlsoccer("api key");         $soccer->setserviceurl("http://www.xmlsoccer.com/footballdata.asmx");         /*  $result=$soccer->getlivescore();*/         $result=$soccer->getfixturesbydateinterval(array( "startdatestring"=> "$yesterday" ,"enddatestring"=> "$today"));                     var_dump($result);          $fp = fopen( "/var/www/public_html/domain/$filedate.json","w+");         fwrite($fp,json_encode($result));         fclose($fp);     }     catch(xmlsoccerexception $e){         echo "xmlsoccerexception: ".$e->getmessage();     } 

the code above stores json data in file :

{ match: [ { id: "346528", date: "2015-07-15t12:00:00+00:00", league: "chinese super league", round: "19", hometeam: "guangzhou evergrande", hometeam_id: "1108", homegoals: "0", awayteam: "henan jianye", awayteam_id: "1100", awaygoals: "0", time: "not started", location: "tianhe stadium", hometeamyellowcarddetails: { }, awayteamyellowcarddetails: { }, hometeamredcarddetails: { }, awayteamredcarddetails: { } }, { id: "346527", date: "2015-07-15t11:45:00+00:00", league: "chinese super league", round: "19", hometeam: "shanghai greenland shenhua", hometeam_id: "1107", homegoals: "0", awayteam: "beijing guoan", awayteam_id: "1098", awaygoals: "0", time: "not started", location: "hongkou football stadium", hometeamyellowcarddetails: { }, awayteamyellowcarddetails: { }, hometeamredcarddetails: { }, awayteamredcarddetails: { } }, } 

right need add fields each match, example if hometeam = real madrid , add field hometeamshort = rmd etc. should done dynamically because have create if's each away , home team.

somebody can how can ?

first have decode json

$array = json_decode($result); 

get match array

$arr = $array->match; 

now loop through objects (i.e.) , whatever necessary

foreach($arr $item) { //foreach element in $arr     //do whatever want object     if($item->hometeam == "real madrid") {        $item->hometeamshort = "rmd";     } }  print_r($arr); //now $arr required result wanted have  //you can convert array json  $arr2 = json_encode($arr); 

please let me know if wanted else , change code address need.

thank you.

* additional example *

check if have put variables , arrays of object before running loop. since can't see have done in code , line numbers are, pasting full dummy code along outputs, please compare own code , should able proper output.

<?php //let's take example json data $result = '{"match":                 [                     {"name":"tom", "age":22},                     {"name":"jerry", "age":20},                     {"name":"hank", "age":25}                 ]                        }';  $array = json_decode($result); $arr = $array->match; 

let's print array once before processing

print_r($arr); //array ( [0] => stdclass object ( [name] => tom [age] => 22 ) [1] => stdclass object ( [name] => jerry [age] => 20 ) [2] => stdclass object ( [name] => hank [age] => 25 ) ) 

now loop thing , add whatever property need whichever object want

foreach($arr $item) { //foreach element in $arr     //do whatever want object     if($item->name == "jerry") {        $item->color = "brown";     } } 

now if print array again see color brown has been added jerry

print_r($arr); //array ( [0] => stdclass object ( [name] => tom [age] => 22 ) [1] => stdclass object ( [name] => jerry [age] => 20 [color] => brown ) [2] => stdclass object ( [name] => hank [age] => 25 ) ) 

convert array json if want

$arr2 = json_encode($arr); 

now, check json, see color brown there against jerry

var_dump($arr2); //string(92) "[{"name":"tom","age":22},{"name":"jerry","age":20,"color":"brown"},{"name":"hank","age":25}]"  ?> 

this step step analysis , explanation of code. can see not getting undefined object error. so, try copy pasting above code , if can replicate same error, easy me understand causing bug.

i have made phpfiddle link here, can check code , run there.

thanks again.


Comments