Carlos
Carlos

Reputation: 425

Accessing PHP Objects

I am trying to access an object data through PHP. For example, if I want to access the WasSuccess object, I will simply code it like so: echo $result->TraceShipmentResult->WasSuccess; However, when I want to call the name data under the Parties object, it becomes more difficult since there are so many types and I don't know how to call a specific one. If I would like to echo the name of the CONSIGNEE, how would I go about it?

<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>

Upvotes: 1

Views: 34

Answers (1)

Taki
Taki

Reputation: 17654

you can access the first element like this :

echo echo $result->Result->Shipment->Parties->Party[0]->Name;

or :

$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 ;
}

Upvotes: 2

Related Questions