Search code examples
phpjsonobjectechovaluestack

Print values form an stdObject inside an Array from JSON Response


Im kinda new with php and JSON, and after digging around for a response I was not able to find one, I was hoping if you could help me…

I am working on a Report from with a JSON Response The JSON response looks somewhat like this:

stdClass Object
(
[FlightInformation] => stdClass Object
    (
   [@size] => 10
     [Itinerary] => Array
       (
         [0] => stdClass Object
           (
            [bookingID] => 123456789
            [creationDate] => 10/04/2012
            [Customer] => stdClass Object
               (
                [email] => [email protected]
               )
               [FlightConfirmation] => stdClass Object
                  (
                    [supplierId] => AA
                    [arrivalDate] => 10/04/2012
                    [departureDate] => 10/05/2012
                    [confirmationNumber] => 0987654321
                    [RateInformation] => stdClass Object
                  (
                    [@size] => 1
                    [RateDescription] => stdClass Object
                        (
                         [@promo] => false
                         [ChargeableInfo] => stdClass Object
                             (
                              [@total] => 57.94

I building a Report that is like this..

foreach( $response->FlightInformation->Itinerary as $output) {

    echo $output-> bookingID;
    echo $output-> creationDate;
    echo $output-> arrivalDate;         <<<< won't Print
    echo $output->departureDate;    <<<< won't Print
    echo $output->total;            <<<< won't Print

and some more elements… but anything that is under an object inside de Itinerary Array won't show.. I cannot get to print thes values indicated, please help…

Cheers


Solution

  • That is because arrival, departure and total are objects themselves. You would have to do something like:

     $output->Customer->FlightConfirmation->arrivalDate;
     $output->Customer->FlightConfirmation->departureDate
     $output->Customer->FlightConfirmation->RateInformation->RateDescription->ChargeableInfo->total;