Tuesday, 13 August 2013

PHP not iterating through nested JSON properly

PHP not iterating through nested JSON properly

I am trying to loop through a JSON Object (DATA) containing three nested
JSON Objects (NEWUSERS,NEWUSERDATA,NEWRELATIONS), using a switch function
to choose the appropriate function for a MySQL insert. Each function
housed in the switch is called once as I loop through the keys, but they
ALL seem to only ever receive the NEWRELATIONS object. Consequently, the
functions fail with the exception of insertNewTestRelations, the function
intended to receive the NEWRELATIONS object. I think the answer must be
staring me in the face, but can anybody think why the JSON object is being
reused?
Reading and iterating JSON
$json=json_decode($_SERVER['HTTP_JSON'],true);
$data=$json['DATA'];
foreach($data as $key=>$json_obj){
$result=null;
$result=$db->insertNewSwitch($key,$json_obj,$last_sync);
$response[$key.":errors"]=$result['errors'];
$response[$key.":successes"]=$result['successes'];
}
Switch function
public function insertNewSwitch($key,$json_obj,$last_sync){
$result;
if($key="NEWUSERS"){
$result=$this->insertNewTestUsers($json_obj,$last_sync,$key);
}
if($key="NEWUSERDATA"){
$result=$this->insertNewTestUserdata($json_obj,$last_sync,$key);
}
if($key="NEWRELATIONS"){
$result=$this->insertNewTestRelations($json_obj,$last_sync,$key);
}
return $result;
}

No comments:

Post a Comment