Navin Blackx
Navin Blackx

Reputation: 11

Foreach Loop for Array of StdClass in Php

I trying to get values of WSResult->Fare->BaseFare and Status->StatusCode using foreach loop Kindly help me....

But i have multiple WsResult like [0], [1] and goes on. This is my Response from my Api.

$h=array();
$h= (array)$cliente->__call('Search',$opta);

Below is output which i got from api

Array (
[SearchResult] => stdClass Object (
    [Status] => stdClass Object (
        [StatusCode] => 02
        [Description] => Successfull
        [Category] => SR
    )
    [Result] => stdClass Object (
        [WSResult] => Array (
            [0] => stdClass Object (
                [TripIndicator] => 1
                [Fare] => stdClass Object (
                    [BaseFare] => 2539
                    [Tax] => 460
                    [ServiceTax] => 0
                    [AdditionalTxnFee] => 152.34
                    [AgentCommission] => 38.08
                    [TdsOnCommission] => 7.62
                    [IncentiveEarned] => 0.00
                    [TdsOnIncentive] => 0.00
                    [PLBEarned] => 0.00
                    [TdsOnPLB] => 0.00
                    [PublishedPrice] => 3156.34
                    [AirTransFee] => 0
                    [Currency] => INR
                    [Discount] => 0
                    [ChargeBU] => stdClass Object (
                        [ChargeBreakUp] => Array (
                            [0] => stdClass Object (
                                [PriceId] => 0
                                [ChargeType] => TboMarkup
                                [Amount] => 5
                            )
                            [1] => stdClass Object (
                                [PriceId] => 0
                                [ChargeType] => OtherCharges
                                [Amount] => 0.00
                            )
                        )
                    )
                    [OtherCharges] => 5.00
                    [FuelSurcharge] => 0
                    [TransactionFee] => 0
                    [ReverseHandlingCharge] => 0
                    [OfferedFare] => 2965.92
                    [AgentServiceCharge] => 0
                    [AgentConvienceCharges] => 0
                )
            )
        )
    )
)
)

How do I get to the BaseFare under Fare and also StatusCode under Status

I tried something like this but didn't work:

foreach($h->SearchResult as $result){
     echo $result->Status->StatusCode;
     echo $result->Result->WSResult->Fare->BaseFare;
}

But Im getting error as "Trying to get property of non-object".

Upvotes: 1

Views: 425

Answers (1)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76395

update
Looking at your updated question, there is a bigger issue with your code. You are calling the magic __call method manually! Don't. Just don't do that.
Replace:

$h= (array)$cliente->__call('Search',$opta);
//with
$h = $cliente->Search($opta);

And var_dump the value of $h right away.
I'm assuming that, because I've removed the cast from your code, you can then easily do something like this:

$baseFares = array();
foreach ($h->SearchResult->Result->WsResult as $k => $fare)
    $baseFares[$k] = $fare->Fare->BaseFare;
var_dump($baseFares);

That should give you an array of all baseFares returned by the API.

The value of $h is not an instance of stdClass. If you look closely at the dump, you can see that you're actually dealing with an array:

Array (//<-- array
    [SearchResult] => stdClass Object (//<-- this is an object

So to get the data you're after, you'll have to write:

$result = $h[0]->SearchResult->Result->WSResult;//this is an array, again
foreach ($result as $r)
    echo 'BaseFare: ', $r->Fare->BaseFare, PHP_EOL;

Upvotes: 1

Related Questions