我有以下代码
<?php
if (is_object($checkAvailability->availableHotels))
$hotelResponse[] = $checkAvailability->availableHotels;
$hotelResponse = $checkAvailability->availableHotels;
foreach ((array)$hotelResponse as $hnum => $hotel)
?>
$hotelResponse
是一个多维数组
print_r($checkAvailability->availableHotels); is generating :
数组([0] =&gt; stdClass对象([processId] =&gt; H0-41925041 [hotelCode] =&gt; ITJHRV [availabilityStatus] =&gt; InstantConfirmation [totalPrice] =&gt; 1421 [totalTax] =&gt; 0 [totalSalePrice] =&gt; 1509.38 [货币] =&gt; EUR [boardType] =&gt;自助早餐[rooms] =&gt;数组([0] =&gt; stdClass对象([roomCategory] =&gt;经典双人或双床Room-1大床[paxes] =&gt;数组([0] =&gt; stdClass对象([paxType] =&gt;成人[年龄] =&gt; 30)[1] =&gt; stdClass对象([paxType] = &gt;成人[年龄] =&gt; 30))[totalRoomRate] =&gt; 1421 [ratesPerNight] =&gt;数组([0] =&gt; stdClass对象([日期] =&gt; 2015-04-20 [金额] =&gt; 203)[1] =&gt; stdClass对象([date] =&gt; 2015-04-21 [amount] =&gt; 203)[2] =&gt; stdClass对象([date] =&gt; 2015- 04-22 [amount] =&gt; 203)[3] =&gt; stdClass对象([date] =&gt; 2015-04-23 [amount] =&gt; 203)[4] =&gt; stdClass对象([date] ] =&gt; 2015-04-24 [amount] =&gt; 203)[5] =&gt; stdClass对象([date] =&g吨; 2015-04-25 [amount] =&gt; 203)[6] =&gt; stdClass对象([date] =&gt; 2015-04-26 [amount] =&gt; 203)))))[1] =&gt; stdClass对象([processId] =&gt; HA-51032431 [hotelCode] =&gt; ITRR5G [availabilityStatus] =&gt; InstantConfirmation [totalPrice] =&gt; 1590 [totalTax] =&gt; 0 [totalSalePrice] =&gt; 0 [货币] =&gt; EUR [boardType] =&gt; Bed&amp; Breakfast [rooms] =&gt;数组([0] =&gt; stdClass对象([roomCategory] =&gt;双床房(包括早餐和Wi-Fi)[paxes] =&gt;数组([0] =&gt; stdClass对象([paxType] =&gt;成人[年龄] =&gt; 30)[1] =&gt; stdClass对象([paxType] =&gt;成人[年龄] =&gt ; 30))[totalRoomRate] =&gt; 1590 [ratesPerNight] =&gt;数组([0] =&gt; stdClass对象([date] =&gt; 2015-04-20 [amount] =&gt; 197)[1] =&gt; stdClass对象([date] =&gt; 2015-04-21 [amount] =&gt; 197)[2] =&gt; stdClass对象([date] =&gt; 2015-04-22 [amount] =&gt ; 239)[3] =&gt; stdClass对象([date] =&gt; 2015-04-23 [amount] =&gt; 239)[4] =&gt; stdClass对象([date] =&gt; 2015-04- 24 [amount] =&gt; 239)[5] =&gt; stdClass对象([date] =&gt; 2015-04-25 [金额] =&GT; 239)[6] =&gt; stdClass对象([date] =&gt; 2015-04-26 [amount] =&gt; 240)))))))
我如何做到这一点通过回复它们得到每个回应
<?php echo $hotel->hotelCode?>
<?php echo $hotel->totalPrice?>
据我所知,我需要将2d数组转换为对象,但从那里我不知道。请帮忙。
答案 0 :(得分:1)
试试这个:
$hotelCodes = array() ;
$availHotels = $checkAvailability->availableHotels ;
foreach($availHotels as $hotel){
$hotelCodes[] = $hotel->hotelCode ;
//echo $hotel->hotelCode ;
//echo $hotel->totalPrice ;
}
现在您拥有$hotelCodes
数组并可以访问$hotelCodes[0]
,$hotelCode[1]
以及更多内容。