使用php访问数组中的对象

时间:2012-12-23 23:31:17

标签: php arrays object

希望你能帮助我。

我尝试过这种不同的方式,但我无法解决这个问题。

我有一个包含对象的数组:

    Array
(
[1] => CntProviderIncentiveResult Object
    (
        [_StartDate:protected] => 1356303600
        [_EndDate:protected] => 1356908400
        [_ProviderRoom:CntProviderIncentiveResult:private] => 1
        [_RoomName:CntProviderIncentiveResult:private] => Doble
        [_RoomCode:CntProviderIncentiveResult:private] => 2370
        [_PropertyCode:CntProviderIncentiveResult:private] => anglers
        [_Occupancy:CntProviderIncentiveResult:private] => 
        [_Currency:CntProviderIncentiveResult:private] => 
        [_AllocationGroup:CntProviderIncentiveResult:private] => 
        [_FreeSaleGroup:CntProviderIncentiveResult:private] => 
        [_StopSaleGroup:CntProviderIncentiveResult:private] => 
        [_ReleaseGroup:CntProviderIncentiveResult:private] => 
        [_MinStayGroup:CntProviderIncentiveResult:private] => 
        [_UpdateRequirement:CntProviderIncentiveResult:private] => 

)  )

我尝试将其中一个对象分配给设置数组键的新数组:

$ConfirmRoom = $ConfirmRoomData[$roomKey];

其中$roomKey是数组的键。 (1,20,21)

在我确认是否做得好之后:

if ($ConfirmRoom instanceof CntProviderIncentiveResult) {echo "OK";} 

但新对象$ConfirmRoom始终为空。

你可以让我看看光吗?

2 个答案:

答案 0 :(得分:1)

首先尝试var_dump($ConfirmRoom)

另外,请确保isset($ConfirmRoomData[$roomKey])并确保大数组确实位于$ConfirmRoomData

这些只是一些可能有助于解决问题的基本调试步骤。我的猜测是$roomKey没有被正确定义。

答案 1 :(得分:1)

最后我解决了:

foreach ($ConfirmRoomData as $key => $value) {
if (is_object ($value)) {
if ($key == $roomKey){
                                            $ConfirmRoom = $value;
}

} }