从嵌套的PHP obj中提取数据

时间:2015-03-07 07:36:31

标签: php arrays

我有数组$ roomResponse:

Array
(
    [0] => Array
        (
            [0] => stdClass Object
                (
                    [roomCategory] => Standard Room - Double
                    [paxes] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [paxType] => Adult
                                    [age] => 30
                                )
                            [1] => stdClass Object
                                (
                                    [paxType] => Adult
                                    [age] => 30
                                )
                            [2] => stdClass Object
                                (
                                    [paxType] => Child
                                    [age] => 11
                                )
                        )
                    [totalRoomRate] => 280
                    [ratesPerNight] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [date] => 2015-06-08
                                    [amount] => 40
                                )
                            [1] => stdClass Object
                                (
                                    [date] => 2015-06-09
                                    [amount] => 40
                                )
                            [2] => stdClass Object
                                (
                                    [date] => 2015-06-10
                                    [amount] => 40
                                )
                        )
                )
            [1] => stdClass Object
                (
                    [roomCategory] => Standard Room - Double
                    [paxes] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [paxType] => Adult
                                    [age] => 30
                                )
                            [1] => stdClass Object
                                (
                                    [paxType] => Child
                                    [age] => 11
                                )
                            [2] => stdClass Object
                                (
                                    [paxType] => Child
                                    [age] => 11
                                )
                        )
                    [totalRoomRate] => 274
                    [ratesPerNight] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [date] => 2015-06-08
                                    [amount] => 40
                                )
                            [1] => stdClass Object
                                (
                                    [date] => 2015-06-09
                                    [amount] => 40
                                )
                            [2] => stdClass Object
                                (
                                    [date] => 2015-06-10
                                    [amount] => 40
                                )
                        )    
                )    
        )

我如何拉取roomCategory和totalRoomRate。以上数组从两个房间的信息中获取信息。我想用as $roomCategory[][]$totalRoomRate[][]

我已经尝试了下面的foreach但它不起作用它返回空数组。

foreach ($roomResponse as $camere) {
        $roomCategory[][] = $camere->roomCategory;
        $totalRoomRate[][] = $camere->totalRoomRate;

由于

3 个答案:

答案 0 :(得分:0)

foreach ($roomResponse[0] as $camere) {
    ...
}

答案 1 :(得分:0)

试试这个

<?php
foreach($roomResponse[0] as $key => $value){
 print_r($key) // Here the keys
 print_r($value) // Here the values
}
?>

答案 2 :(得分:0)

这可以让你根据需要抓住东西:

// Reconstruct for testing...
$oRoom1 = new stdClass;
$oPaxes1 = new stdClass;
$oPaxes2 = new stdClass;
$oPaxes3 = new stdClass;
$oPaxes1->paxType = 'Adult';
$oPaxes1->age = '30';
$oPaxes2->paxType = 'Child';
$oPaxes2->age = '15';
$oPaxes3->paxType = 'Adult';
$oPaxes3->age = '30';
$oRates1 = new stdClass;
$oRates2 = new stdClass;
$oRates3 = new stdClass;
$oRates1->date = '2015-06-08';
$oRates1->amount = 40;
$oRates1->date = '2014-06-08';
$oRates1->amount = 50;
$oRates1->date = '2013-06-08';
$oRates1->amount = 20;
$aRatesPerNight[ 0 ] = $oRates1;
$aRatesPerNight[ 1 ] = $oRates2;
$aRatesPerNight[ 2 ] = $oRates3;


$oRoom1->roomCategory = 'Standard Room - Double';
$oRoom1->paxes[ 0 ] = $oPaxes1;
$oRoom1->paxes[ 1 ] = $oPaxes2;
$oRoom1->paxes[ 2 ] = $oPaxes3;
$oRoom1->totalRoomRate = 280;
$oRoom1->ratesPerNight = $aRatesPerNight;

$aComplex[] = $oRoom1;
$aComplex[] = $oRoom1;

$iCountRooms = count( $aComplex );
for( $i = 0; $i < $iCountRooms; ++$i )
{
    foreach( $aComplex[ $i ] as $sRoom => $aRoomAttributes )
    {
        if( $sRoom = 'roomCategory' )
        {
            echo 'Room Category is: ' . $aComplex[ $i ]->$sRoom;
            echo '<br />';
        }
        if( $sRoom = 'totalRoomRate' )
        {
            echo 'Total Room Rate is: ' . $aComplex[ $i ]->$sRoom;
            echo '<br />';
        }
    }
}
// How can i pull roomCategory and totalRoomRate.
echo '<pre>';
var_dump( $aComplex );
echo '</pre>';