从 PHP 对象中提取数据

Pull data from a 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

"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。