我正在尝试通过PHP访问对象数据。例如,如果我想访问WasSuccess对象,我将简单地对其进行编码:echo $ result-> TraceShipmentResult-> WasSuccess;但是,当我想在Parties对象下调用名称数据时,由于类型太多而我不知道如何调用特定的类型,因此变得更加困难。如果我想回应一下CONSIGNEE的名字,我该怎么办呢?
<TraceShipmentResult>
<CustomerData/>
<WasSuccess>true</WasSuccess>
<Messages/>
<Result>
<Shipment>
<ErrResponse xmlns="http://tempuri.org/">
<Level>0</Level>
<Message/>
</ErrResponse>
<Parties xmlns="http://tempuri.org/">
<Party>
<PartyID/>
<Type>CONSIGNEE</Type>
<Attention/>
<Name>H T HACKNEY CO</Name>
<PrimaryAddress>
<Address1>3580 NW 119TH ST</Address1>
<Address2/>
<Address3/>
<City>MIAMI</City>
<State>FL</State>
<Zip>33167-2928</Zip>
<CountryName/>
<CountryCode>USA</CountryCode>
<AddressType/>
</PrimaryAddress>
</Party>
<Party>
<PartyID/>
<Type>SHIPPER</Type>
<Attention/>
<Name>EXPRESS TRANSPORT BY AIR LLC</Name>
<PrimaryAddress>
<Address1>870 SPRINGFIELD RD S</Address1>
<Address2/>
<Address3/>
<City>UNION</City>
<State>NJ</State>
<Zip>07083-8614</Zip>
<CountryName/>
<CountryCode>USA</CountryCode>
<AddressType/>
</PrimaryAddress>
</Party>
<Party>
<PartyID/>
<Type>BILLER</Type>
<Attention/>
<Name>WORLDWIDE EXPRESS</Name>
<PrimaryAddress>
<Address1>2323 VICTORY AVE STE 1600</Address1>
<Address2/>
<Address3/>
<City>DALLAS</City>
<State>TX</State>
<Zip>75219</Zip>
<CountryName/>
<CountryCode>USA</CountryCode>
<AddressType/>
</PrimaryAddress>
</Party>
</Parties>
.
.
.
</StatusHistory>
<ErrorMessage xmlns="http://tempuri.org/"/>
</Shipment>
</Result>
</TraceShipmentResult>
答案 0 :(得分:2)
你可以像这样访问第一个元素:
echo echo $result->Result->Shipment->Parties->Party[0]->Name;
或:
$parties = echo $result->Result->Shipment->Parties->Party ;
foreach($parties as $key => $value){
if($value->Type == "CONSIGNEE") // i'm guessing CONSIGNEE is a type
echo $value->Name ;
}