I'm dealing with a SOAP client response for a flight booking application, I successfully got the response, See the response below:

<arzoo__response>
<Response__Depart>
    <OriginDestinationOptions>
        <OriginDestinationOption>
            <FareDetails>
                <ChargeableFares>
                    <ActualBaseFare>4850</ActualBaseFare>
                    <Tax>4267</Tax>
                    <STax>31</STax>
                    <SCharge>0</SCharge>
                    <TDiscount>0</TDiscount>
                    <TPartnerCommission>0</TPartnerCommission>
                </ChargeableFares>
                <NonchargeableFares>
                    <TCharge>0</TCharge>
                    <TMarkup>0</TMarkup>
                    <TSdiscount>0</TSdiscount>
                </NonchargeableFares>
            </FareDetails>
            <FlightSegments>
                <FlightSegment>
                    <AirEquipType>321</AirEquipType>
                    <ArrivalAirportCode>DEL</ArrivalAirportCode>
                    <ArrivalDateTime>2013-05-20T08:00:00</ArrivalDateTime>
                    <DepartureAirportCode>BOM</DepartureAirportCode>
                    <DepartureDateTime>2013-05-20T06:00:00</DepartureDateTime>
                    <FlightNumber>601</FlightNumber>
                    <OperatingAirlineCode>AI</OperatingAirlineCode>
                    <OperatingAirlineFlightNumber>601</OperatingAirlineFlightNumber>
                    <RPH></RPH>
                    <StopQuantity>0</StopQuantity>
                    <airLineName>Air India</airLineName>
                    <airportTax>4267</airportTax>
                    <imageFileName>http://live.arzoo.com/FlightWS/image/AirIndia.gif</imageFileName>
                    <viaFlight></viaFlight>
                    <BookingClass> 
                        <Availability>4</Availability>
                        <ResBookDesigCode>U</ResBookDesigCode>
                    </BookingClass>
                    <BookingClassFare>
                        <adultFare>4850</adultFare>
                        <bookingclass>U</bookingclass>
                        <classType>Economy</classType>
                        <farebasiscode>fjyS3YyUlEusLfJ4bwgPvQ==</farebasiscode>
                        <Rule>This fare is Refundable &lt;br&gt; Baggage : 25K&lt;br&gt;Booking Class : U|Re-Schedule Charges: Rs. 750 per sector + Fare difference (If any) +admin fee 500 + Service Fee of Rs. 250 Sector .|Cancellation Charges : Basic fare +Airline administration fee 500 + Service Charges 250 Per Passenger Per Sector . |</Rule>
                        <adultCommission>0</adultCommission>
                        <childCommission>0</childCommission>
                        <commissionOnTCharge>0</commissionOnTCharge>
                    </BookingClassFare>
                    <Discount>0</Discount>
                    <airportTaxChild>0</airportTaxChild>
                    <airportTaxInfant>0</airportTaxInfant>
                    <adultTaxBreakup>2800,147,1320</adultTaxBreakup>
                    <childTaxBreakup>0,0,0</childTaxBreakup>
                    <infantTaxBreakup>0,0,0</infantTaxBreakup>
                    <octax>0</octax>
                </FlightSegment> 
            </FlightSegments>
            <id>arzoo11</id>
            <key>wtZcSVMY/gphWFSOTFWg8oKRnosq3p9wt7R4SjMB0EUK8sDjVS91GicTJzH+TWN+pNURIyTJYKOW O8yH8+0tzpA4t8aEEvzaOE6ZnTtBotFDwLtSiN0xXqMsaDl8diV51l7d9ata/3rxTgfh9d8ZSmFY VI5MVaDywdHNcjAR1vwnEycx/k1jftlsnmWWqYGnJxMnMf5NY36YIq2FtLkfcHtsd+IDEhxpxJuT v4YyS+QnEycx/k1jftlsnmWWqYGnJxMnMf5NY37ZbJ5llqmBpycTJzH+TWN+GlZsae+KA6L3dlms eLkXZFKXPvd5FAnBXnTfRGc5sz+UC8CsZNzDV2FYVI5MVaDyFiThuG7WKPhzqWDbQb1E1IqUKT5I FtF6OaCYjb6EFGhDwLtSiN0xXt7N3OmPGVH2v18OFlhFoI+A/uAfnTovYr9fDhZYRaCPgP7gH506 L2K/Xw4WWEWgj4D+4B+dOi9iv18OFlhFoI+A/uAfnTovYr9fDhZYRaCPgP7gH506L2K/Xw4WWEWg j4D+4B+dOi9iv18OFlhFoI/87KPqwdBsCm4nF4DlZwRNQ/zni8QE42g8S0GV1z3SJXaOdoTFisn9 v18OFlhFoI+ThOYsjNAbXEP854vEBONoPEtBldc90iVD/OeLxATjaAwTg0ygCEBmEgnKEhG4Md1D /OeLxATjaDxLQZXXPdIlQ/zni8QE42jXGTnbB/ydvR4daAuN34kWYVhUjkxVoPJtRWbi6bZrXL9f DhZYRaCPgP7gH506L2K/Xw4WWEWgj4D+4B+dOi9iv18OFlhFoI8KriAmVoEyR3OpYNtBvUTUp1Mf I240+obPusGaAOeuxg==</key>
        </OriginDestinationOption>
    </OriginDestinationOptions>
</Response__Depart>
</arzoo__response>

Now i have to display them as :

Base Price: 4850
Tax: 31
STax: 31
SCharge: 0
TDiscount: 0
TPartnerCommission: 0
.....................
Arrival Airport Code: DEL
.....................

I tried several methods using foreach():

   $result = $client->__call('getAvailability',array($test));   
   $xml = simplexml_load_string($result);
    foreach($xml->OriginDestinationOption as $od){
        foreach($od->FareDetails as $fd){
            foreach($fd->ChargeableFares as $cf){
                echo $cf->ActualBaseFare;
            }
        }
    }

but never sot it out. Can any one please help me out on this ?
Your help must appreciated. Thanks in advance

Member Avatar for diafol
 foreach($xml->Response__Depart->OriginDestinationOptions->OriginDestinationOption as $od){

I tried:

    foreach($arr->Response__Depart->OriginDestinationOptions->OriginDestinationOption as $od){
        print_r($od);
    }

but it was giving the same error:

Notice: Trying to get property of non-object in C:\xampp\htdocs\Flight\availability.php on line 36

Notice: Trying to get property of non-object in C:\xampp\htdocs\Flight\availability.php on line 36

Notice: Trying to get property of non-object in C:\xampp\htdocs\Flight\availability.php on line 36

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Flight\availability.php on line 36

The line(36) is:
foreach($arr->Response__Depart->OriginDestinationOptions->OriginDestinationOption as $od){

Why are you using $arr, your variable is, as Diafol has pointed out -> $xml

Member Avatar for diafol

How about this?

   //$result = $client->__call('getAvailability',array($test)); //obviously I had to use your xml from above for this  
   $xml = simplexml_load_string($result);
    foreach($xml->Response__Depart->OriginDestinationOptions->OriginDestinationOption as $od){
        $abf = $od->FareDetails->ChargeableFares->ActualBaseFare;
        echo $abf;
    }

Can the <OriginDestinationOption> contain more than one <FareDetails>?
Can the <FareDetails> contain more than one <ChargeableFares>?
Can the <ChargeableFares> contain more than one <ActualBaseFare>?

If the answer to aall those questions is NO, then I think my code should work.

commented: Yah ! Its worked ! Thanks alot.. You are my posting guru.. Learning lot from you guys +2

In my response <OriginDestinationOption> contain only one <FareDetails>
<FareDetails> contain only one <ChargeableFares>
<ChargeableFares> Contain only one <ActualBaseFare>

Thanks alot :-) Love you all....

Member Avatar for diafol

I know this is solved, but I thought I'd add a little something. Many find stdClass data difficult to traverse, so I thought I'd include a stdClass (Object) -> Multidimensional Array script. However, on quick search, I found one here: http://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/

function objectToArray($d) {
    if (is_object($d)) {
        $d = get_object_vars($d);
    }
    if (is_array($d)) {
        return array_map(__FUNCTION__, $d);
    }
    else {
        return $d;
    }
}

$p = objectToArray($xml);
echo "<pre>";
print_r($p);
echo "</pre>";

So if you want to deal with the xml output as a multidimensional array, this should do it. Personally, I can't see much difference for retrieval, but I suppose, it's what you like.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.