如何从对象中访问数组?

时间:2011-10-29 17:34:11

标签: php

  

可能重复:
  Accessing Class Properties with Spaces

我有和对象文件,我无法获取设备列表,如何获得设备 ??如何拨打“佳能相机”这样的2个病房?

  [Camera] => stdClass Object
    (
        [Canon Camera] => stdClass Object
            (
                [DEVICES] => Array
                    (
                        [0] => Canon EOS 30
                        [1] => Canon EOS 5D Mark II
                        [2] => Canon EOS 7D
                        [3] => Canon EOS A2
                        [4] => Canon EOS Digital Rebel (300D)
                        [5] => Canon EOS Rebel XS
                        [6] => Canon PowerShot 1200
                        [7] => Canon PowerShot A200
                        [8] => Canon PowerShot A520
                        [9] => Canon PowerShot A550


                    )

            )

4 个答案:

答案 0 :(得分:1)

尝试:

$object->Camera->{'Canon Camera'}->DEVICES

然后您将可以访问DEVICES数组。

foreach ( $object->Camera->{'Canon Camera'}->DEVICES as $device ) {
  echo $device . <br/>;
}

答案 1 :(得分:0)

删除数组中的空格,将Canon Camera设为canon_camera

之后,你可以打开它:

$camera->canon_camera->DEVICES

答案 2 :(得分:0)

您可以使用

$object->Camera->{'Canon Camera'}->DEVICES

答案 3 :(得分:0)

所以你试图做这样的事情?:

foreach($devices_list->Camera->{'Canon Camera'}->DEVICES as $key => $value) {
  echo $value."\n";
}