PHP访问对象的数据

PHP访问对象的数据,php,arrays,object,oop,Php,Arrays,Object,Oop,我有一个我想从中获取数据的对象,这个对象看起来像这样 Product Object ( [name] => Simon Test Cup [code] => 123456789 [category_id] => 3 [range_id] => 26 [price] => 10.00 [price_logo_add] => 0.25 [image_id] => 846 [rank] =

我有一个我想从中获取数据的对象,这个对象看起来像这样

    Product Object
(
    [name] => Simon Test Cup
    [code] => 123456789
    [category_id] => 3
    [range_id] => 26
    [price] => 10.00
    [price_logo_add] => 0.25
    [image_id] => 846
    [rank] => 
    [special_offer] => N
    [cartProps] => Array
        (
        )

    [section] => 
    [vatPercentage] => 17.5
    [id] => 551
    [date_created] => 2010-05-25 12:46:57
    [last_updated] => 2010-05-25 14:10:48
    [user_id_updated] => 0
    [_aliases] => Array
        (
            [id] => 551
            [date_created] => 2010-05-25 12:46:57
            [date_updated] => 2010-05-25 14:10:48
            [user_id_updated] => 0
            [name] => Simon Test Cup
            [code] => 123456789
            [category_id] => 3
            [range_id] => 26
            [price] => 10.00
            [price_logo_add] => 0.25
            [image_id] => 846
            [range_image_id] => 848
            [main_image_id] => 847
            [rank] => 
            [special_offer] => N
        )

    [_default] => Array
        (
            [special_offer] => N
        )

    [_related] => Array
        (
            [_related] => Array
                (
                    [range] => stdClass Object
                        (
                            [key] => range
                            [group] => _related
                            [foreignKey] => range_id
                            [indexName] => id
                            [tableName] => cc_range
                            [objectName] => Range
                            [userFieldlyColName] => name
                            [criteria] => id='{%range_id%}'
                            [sqlPostfix] => 
                            [populateOnLoad] => 
                            [objects] => Array
                                (
                                    [26] => Range Object
                                        (
                                            [name] => Shot glasses
                                            [url_name] => shot-glasses
                                            [description] => Personalized shot glasses make great commemorative gifts, souvenirs and wedding favours. Just select your favourite shape and send us a customization form with your logo. See our glassware sale page for info on free logo origination.
                                            [leader] => Customized shot glasses make great commemorative gifts, promotional items and wedding favours.  Individual gift boxes are available so you can give the glasses away easily.
                                            [category_id] => 3
                                            [site_id_csv] => 
                                            [image_id_main] => 565
                                            [image_id_thumb] => 566
                                            [rank] => 
                                            [site] => main
                                            [id] => 26
                                            [date_created] => 2008-05-18 21:39:52
                                            [last_updated] => 2009-02-03 13:49:10
                                            [user_id_updated] => 0
                                            [_aliases] => Array

我想从
[range]=stdClass对象中获取id,正如您可能看到的,您的
产品
对象有一个属性
range\u id

Product Object
(
    [name] => Simon Test Cup
    [code] => 123456789
    [category_id] => 3
    [range_id] => 26          // <--- here!
    [price] => 10.00
    ...

但是可能,
Product
Range
类定义了允许您访问这些信息的方法,您应该使用它们。您只需阅读文档,了解如何使用它们

$product->_related["_related"]["range"]->key

但是我不确定我是否理解得很好,你可以说$object->_-related[“_-related”][“range”],但是这个结构大概是一个对象的结构,它有访问器,可以访问你可能想要的东西。你显然不想担心事物的内部结构

假设Felix的答案不起作用(这可能很好),我猜
\u相关的
字段是受保护的。在这种情况下,该类中应该有一个访问器方法,以便获取相关对象。请在对象上使用
get\u class\u methods()
,并使用可用的方法编辑文章

$product->_related["_related"]["range"]->key