从PHP对象中提取数据

时间:2015-01-25 18:05:03

标签: php object

我正在尝试从PHP对象中提取数据,但我无法获取所需的所有数据。

对象$hotel1是:

stdClass Object ( 
[processId] => HH-24896940 
[hotelCode] => TRN367 
[availabilityStatus] => InstantConfirmation 
[totalPrice] => 398 
[totalTax] => 0 
[totalSalePrice] => 0 
[currency] => EUR 
[boardType] => Half Board 
[rooms] => Array ( [0] => stdClass Object ( 
[roomCategory] => Double Standard [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) 
[1] => stdClass Object ( 
[paxType] => Adult [age] => 30 ) ) 
[totalRoomRate] => 398 
[ratesPerNight] => Array ( [0] => stdClass Object ( 
[date] => 2015-01-27 
[amount] => 398 ) ) ) ) )

使用的代码是:

<?php
  foreach ($availHotels as $hotel1) {
        if ($hotel1->hotelCode === $code1) {
            break;
        }
    }
?>

我用以下数据提取数据:

  $hotel1->boardType , $hotel1->totalPrice

但是当我试图让roomCategory无法使用

$hotel1->roomCategory

1 个答案:

答案 0 :(得分:2)

“roomCategory”嵌套在“room”中。 以下是如何获得预期的示例:

$obj = (object) array(
    'boardType' => 'foo',
    'room' => array(
        (object) array(
            'roomCategory' => 'bar'
        )
    )
);

print_r($obj);

echo $obj->boardType;

echo $obj->room[0]->roomCategory;
在你的情况下,$ obj将是$ hotel1。