如何才能访问'类型'以下结构中的项目?
以下是从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
)
)
)
)
)
)
答案 0 :(得分:0)
根据PHP的版本,假设对象具有适当的getter,您可以这样做:
$item->getCore()->cart->getItems()[3]->getItemOptions()['type'];
如果他们没有吸气者,您只能从相应的班级内部访问Cartthrob_item_product::core
,Cartthrob_cart::items
和Cartthrob_item_product::item_options
,因为他们是protected
。
另一种可能性是索引3处的Cartthrob_item_product
与$ item是同一个对象。在这种情况下,假设对象具有适当的吸气剂,您只需要这样做:
$item->getItemOptions()['type'];