多维数组 - 获取结构中项目的访问权限

时间:2016-12-29 10:53:50

标签: php foreach

如何才能访问'类型'以下结构中的项目?

以下是从foreach输出的变量$ item(print_r($item);的输出)

Cartthrob_item_product Object ( 
    [core:protected] => Cartthrob_core_ee Object ( 
        [cart] => Cartthrob_cart Object ( 
            [items:protected] => Array ( 
                [3] => Cartthrob_item_product Object ( 
                    [item_options:protected] => Array ( 
                        [type] => product  
                    )
                )
            )
        )
    )
)

1 个答案:

答案 0 :(得分:0)

根据PHP的版本,假设对象具有适当的getter,您可以这样做:

$item->getCore()->cart->getItems()[3]->getItemOptions()['type'];

如果他们没有吸气者,您只能从相应的班级内部访问Cartthrob_item_product::coreCartthrob_cart::itemsCartthrob_item_product::item_options,因为他们是protected

另一种可能性是索引3处的Cartthrob_item_product与$ item是同一个对象。在这种情况下,假设对象具有适当的吸气剂,您只需要这样做:

$item->getItemOptions()['type'];