codehelp codehelp - 2 months ago 8
PHP Question

print multiple values for same parameter from an std object response

I would like to print the values of [UpdateDescription] from both shipments in [0] and [1]

Here is the request sent

$auth_call = $soapClient->TrackShipments($params);


Shipments numbers are sent in the $params array

the response is below

stdClass Object
(
[Transaction] => stdClass Object
(
[Reference1] =>
[Reference2] =>
[Reference3] =>
[Reference4] =>
[Reference5] =>
)

[Notifications] => stdClass Object
(
)

[HasErrors] =>
[TrackingResults] => stdClass Object
(
[KeyValueOfstringArrayOfTrackingResultmFAkxlpY] => Array
(
[0] => stdClass Object
(
[Key] => 52398174132
[Value] => stdClass Object
(
[TrackingResult] => stdClass Object
(
[WaybillNumber] => 52398174132
[UpdateCode] => SH005
[UpdateDescription] => Delivered
[UpdateDateTime] => 2016-09-26T14:45:00
[UpdateLocation] => New York
[Comments] => Maz
[ProblemCode] =>
)

)

)

[1] => stdClass Object
(
[Key] => 43559827323
[Value] => stdClass Object
(
[TrackingResult] => stdClass Object
(
[WaybillNumber] => 43559827323
[UpdateCode] => SH008
[UpdateDescription] => Shipment on Hold
[UpdateDateTime] => 2016-09-28T10:18:00
[UpdateLocation] => New York
[Comments] => Na
[ProblemCode] =>
)

)

)

)

)

)


I am currently able to print the value of [UpdateDescription] using the below when theres only 1 shipment as [0] and [1] are not there in the response

$description = $auth_call->TrackingResults-KeyValueOfstringArrayOfTrackingResultmFAkxlpY->Value->TrackingResult-UpdateDescription;
print_r($description);

Answer

Try to use a foreach-loop:

foreach($auth_call->TrackingResults->KeyValueOfstringArrayOfTrackingResultmFAkxlpY as $tracking_result) {
    print_r ($tracking_result->Value->TrackingResult->UpdateDescription);
}