Problem with accessing class members as an array in PHP class
I'm trying to integrate the FedE开发者_运维技巧x tracking class into my application. The class takes a SOAP response and puts it into a hierarchical class structure. I access the values like this:
$city = $response->TrackDetails->Events[$i]->Address->City;
This works fine for most cases. The problem I am having is when there is just one event for a given shipment, I get an error saying that I cannot treat the class as an array. Since there is just one event, I need to access it without the array index using:
$city = $response->TrackDetails->Events->Address->City;
Is there a way to deal with this without doing something like this:
if($num_events==1){
$city = $response->TrackDetails->Events->Address->City;
}else{
$city = $response->TrackDetails->Events[$i]->Address->City;
}
There are a ton of data fields that fall into this issue, so I don't want to use something so cumbersome if I can avoid it. Any ideas?
if ($num_events == 1) {
$response->TrackDetails->Events = array($response->TrackDetails->Events);
}
This can be done with a loop over all the fields in your answer, automatically putting each loner into an array of length 1.
精彩评论