PHP如何访问对象数组 [英] php how to access object array
本文介绍了PHP如何访问对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从以下对象数组访问项目数组
How to access the item array from the following object array
Cart66Cart Object
(
[_items:Cart66Cart:private] => Array
(
[2] => Cart66CartItem Object
(
[_productId:Cart66CartItem:private] => 327
[_quantity:Cart66CartItem:private] => 3
[_optionInfo:Cart66CartItem:private] =>
[_priceDifference:Cart66CartItem:private] => 0
[_customFieldInfo:Cart66CartItem:private] =>
[_productUrl:Cart66CartItem:private] => http://localhost/odesk/cart66/fran-wilson-aloe-lip-care/
[_formEntryIds:Cart66CartItem:private] => Array
(
)
)
[3] => Cart66CartItem Object
(
[_productId:Cart66CartItem:private] => 368
[_quantity:Cart66CartItem:private] => 2
[_optionInfo:Cart66CartItem:private] =>
[_priceDifference:Cart66CartItem:private] => 0
[_customFieldInfo:Cart66CartItem:private] =>
[_productUrl:Cart66CartItem:private] => http://localhost/odesk/cart66/beauty-strokes-basic-shadow-brush/
[_formEntryIds:Cart66CartItem:private] => Array
(
)
)
)
[_promotion:Cart66Cart:private] =>
[_promoStatus:Cart66Cart:private] => 0
[_shippingMethodId:Cart66Cart:private] => 13
[_liveRates:Cart66Cart:private] => Cart66LiveRates Object
(
[toZip] =>
[weight] =>
[rates] => Array
(
)
[_toCountryCode:protected] =>
)
)
推荐答案
类似这样的事情:
$object->_items[index]->_productId
但是,如果_items是私有的,那么您将需要一个公共获取器或将Reflection类弄乱.您可以将私有属性设置为可通过ReflectionProperty访问 试试这个:
But if _items is private you will need a public getter or mess with the Reflection classes. You can set the the private property to be accessible through ReflectionProperty Try this:
$reflectionObject = new ReflectionObject($yourObject);
$property = $reflectionObject->getProperty('_items');
$property->setAccessible(true);
$items = $property->getValue($yourObject);
这篇关于PHP如何访问对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文