1

I was trying a shipment API. The API returns object. But when I try to access a certain value using a simple foreach loop, it does not work as intended. I tried different methods but everything gives me a blank page.

Following is the response received from the API.

stdClass Object
(
[ConsignmentTrackEvents_Details_NewResult] => stdClass Object
    (
        [ConsignmentTrack] => Array
            (
                [0] => stdClass Object
                    (
                        [ERROR] => 
                        [DOCKNO] => 888888
                        [TRANSIT_LOCATION] => Location, Location
                        [ACTIVITY] => Out For Delivery
                        [EVENTDATE] => 22 Jul 2016
                        [EVENTTIME] => 21:05:48
                        [NEXT_LOCATION] => Coimbatore
                        [TRACKING_CODE] => O
                    )

                [1] => stdClass Object
                    (
                        [ERROR] => 
                        [DOCKNO] => 888888
                        [TRANSIT_LOCATION] => Location, Location
                        [ACTIVITY] => Picked up and Booking processed 
                        [EVENTDATE] => 06 Jun 2016
                        [EVENTTIME] => 16:40:39
                        [NEXT_LOCATION] => 
                        [TRACKING_CODE] => B
                    )

            )

    )

)

Then I used the following loop to access the values.

foreach($result['ConsignmentTrackEvents_Details_NewResult'] ['ConsignmentTrack'] as $res){

echo $res['DOCKNO'];

}

Am I doing anything wrong here?

4
  • 2
    That's an object not an array. Use object notation, not array notation. Commented Jul 28, 2016 at 12:37
  • 1
    Change the following foreach($result->ConsignmentTrackEvents_Details_NewResult->ConsignmentTrack as $res) and echo $res->DOCKNO; Commented Jul 28, 2016 at 12:38
  • @JohnConde Yes, now I understand it. Commented Jul 28, 2016 at 12:54
  • @ThinkDifferent Thank you Commented Jul 28, 2016 at 12:54

2 Answers 2

2

Array and Object are different things, you access array entry with $array['key'] and object property with $object->key

foreach($result->ConsignmentTrackEvents_Details_NewResult->ConsignmentTrack as $res){
    echo $res->DOCKNO;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much Ianis for the help and the clarification. I have been learning PHP as part of a project and only learned up to Arrays so far and this code confused me with it.
0

You could Loop through the Main Collection with a foreach Loop and then drill down to object contained within it, which (in turn) contains the data you are looking for.

<?php
   // $result HERE IS THE MAIN DATA...
    foreach($result as $key=>$data){
        $trackResult    = $result['ConsignmentTrackEvents_Details_NewResult'];
        foreach($trackResult as $intKey=>$objData){
            $dockNo         = $objData->DOCKNO;
            $transit        = $objData->TRANSIT_LOCATION;
            $activity       = $objData->ACTIVITY;
            $eventDate      = $objData->EVENTDATE;
            $eventTime      = $objData->EVENTTIME;
            $nextLocation   = $objData->NEXT_LOCATION;
            $trackingCode   = $objData->TRACKING_CODE;

            echo $dockNo;

        }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.