我尝试使用foreach循环获取WSResult-> Fare-> BaseFare和Status-> StatusCode的值 请帮助我......
但我有多个WsResult,如[0],[1]并继续。 这是我对Api的回复。
$h=array();
$h= (array)$cliente->__call('Search',$opta);
以下是我从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
)
)
)
)
)
)
如何访问BaseFare
下的Fare
以及StatusCode
下的Status
我试过这样的事情,但没有工作:
foreach($h->SearchResult as $result){
echo $result->Status->StatusCode;
echo $result->Result->WSResult->Fare->BaseFare;
}
但我得到错误"试图获得非对象的属性"。
答案 0 :(得分:1)
更新
查看更新后的问题,您的代码存在更大的问题。您正在手动调用魔术__call
方法 !唐'吨。不要那样做。
替换:
$h= (array)$cliente->__call('Search',$opta);
//with
$h = $cliente->Search($opta);
并var_dump
立即$h
的价值
我假设,因为我已经从您的代码中移除了演员,您可以轻松地执行以下操作:
$baseFares = array();
foreach ($h->SearchResult->Result->WsResult as $k => $fare)
$baseFares[$k] = $fare->Fare->BaseFare;
var_dump($baseFares);
这应该给你一个由API返回的所有baseFares的数组。
$h
的值不是 stdClass
的实例。如果你仔细观察转储,你会发现你实际上正在处理一个数组:
Array (//<-- array
[SearchResult] => stdClass Object (//<-- this is an object
为了获得您之后的数据,您必须写下:
$result = $h[0]->SearchResult->Result->WSResult;//this is an array, again
foreach ($result as $r)
echo 'BaseFare: ', $r->Fare->BaseFare, PHP_EOL;