Reputation: 61
I have an xml response for a DHL tracking and i want to echo specific elements on my php page.
I use the following code to print out the tracking results without formatting:
print_r($response);
The xml response looks like this:
Array
(
[TrackingResponse] => Array
(
[xmlns:req] => http://www.dhl.com
[xmlns:xsi] => http://www.w3.org/2001/XMLSchema-instance
[xsi:schemaLocation] => http://www.dhl.com TrackingResponse.xsd
[Response] => Array
(
[ServiceHeader] => Array
(
[MessageTime] => 2013-12-12T11:51:05+00:00
[MessageReference] => j2xfhcBpCE2yd9gbeC5tjqxIX8xjDpZ1
[SiteID] => iraqnova
)
)
[AWBInfo] => Array
(
[AWBNumber] => 8564385550
[Status] => Array
(
[ActionStatus] => success
)
[ShipmentInfo] => Array
(
[OriginServiceArea] => Array
(
[ServiceAreaCode] => FRA
[Description] => FRANKFURT - GERMANY
)
[DestinationServiceArea] => Array
(
[ServiceAreaCode] => MLA
[Description] => MALTA - MALTA
)
[ShipperName] => STANDARD CHARTERED BANK
[ShipperAccountNumber] => 144193851
[ConsigneeName] => BANK OF VALLETTA P.L.C
[ShipmentDate] => 2013-02-14T15:14:00
[Pieces] => 1
[Weight] => 0.08
[WeightUnit] => K
[GlobalProductCode] => U
[ShipmentDesc] => 1402130018
[DlvyNotificationFlag] => Y
[Shipper] => Array
(
[City] => Frankfurt/Main
[PostalCode] => 60486
[CountryCode] => DE
)
[Consignee] => Array
(
[City] => Santa Venera
[PostalCode] => 9030
[CountryCode] => MT
)
[ShipperReference] => Array
(
[ReferenceID] => Doc
)
)
)
)
)
I'm getting lost with so many foreach loops to get to the specific xml tags inside the [ShipmentInfo] tag:
foreach($response as $tag){
echo $tag['ShipmentInfo'];
}
The sample tracking number and info, from the DHL XML Service Validation website http://xmlpitest-ea.dhl.com/serviceval/jsps/main/Main_menu.jsp
Thank you,
Upvotes: 0
Views: 623
Reputation:
print_r($response ['TrackingResponse']['AWBInfo']['ShipmentInfo']);
or if you don't know the path: (pseudocode)
function findAndEcho($obj,$tag) {
for $obj as $key => $val {
if $key = $tag {
print_r($val)
}
else if is_array($val) {
findAndEcho($val,$tag)
}
}
}
Upvotes: 0
Reputation: 26722
$arr['TrackingResponse']['AWBInfo']['ShipmentInfo']
will lead to the shipment info and then iterate over this using foreach
.
Like -
if(is_array($arr['TrackingResponse']['AWBInfo']['ShipmentInfo'])) {
foreach(is_array($arr['TrackingResponse']['AWBInfo']['ShipmentInfo']) as $shiptagkey=>$shiptagval)
{
echo $shiptagkey, " ", $shiptagval;
}
}
Although $shiptagval
itself going to be an array so you need to care about this as well.
Upvotes: 1