如何从PHP对象数组中检索项目?

时间:2012-09-09 19:13:19

标签: php arrays object

我想从这个数组中检索数量。

  invoice Object
    (
    [data:private] => Array
    (
    [i_status] => pend
    [i_title] => 500 HLCoins , 500 HLCoins x8
    [i_member] => 1
    [i_items] => Array
    (
    [0] => Array
    (
    [act] => new
    [app] => nexus
    [type] => product
    [cost] => 0
    [tax] => 0
    [renew_term] => 0
    [renew_units] =>
    [renew_cost] => 0
    [quantity] => 1
    [physical] =>
    [shipping] => Array
    (
    )
    [weight] => 0
    [itemName] => 500 HLCoins
    [itemID] => 3
    [cfields] => Array
    (
    )
    [extra] =>
    [opt_id] => 0
    [associated] =>
    [assocBought] =>
    [groupRenewals] => 0
    [methods] => Array
    (
    )
    [k] => 0
    [_tax] => 0
    )
    [1] => Array
    (
    [act] => new
    [app] => nexus
    [type] => product
    [cost] => 0
    [tax] => 0
    [renew_term] => 0
    [renew_units] =>
    [renew_cost] => 0
    [quantity] => 8
    [physical] =>
    [shipping] => Array
    (
    )
    [weight] => 0
    [itemName] => 500 HLCoins
    [itemID] => 3
    [cfields] => Array
    (
    )
    [opt_id] => 0
    [groupRenewals] => 0
    [methods] => Array
    (
    )
    [_tax] => 0
    )
    )
    [i_total] => 0
    [i_date] => 1347217384
    [i_return_uri] =>
    [i_paid] => 0
    [i_status_extra] => a:1:{s:4:"type";s:4:"zero";}
    [i_discount] => 0
    [i_temp] =>
    [i_ordersteps] => 0
    [i_noreminder] => 1
    [i_renewal_ids] => Array
    (
    )
    [i_po] =>
    [i_notes] =>
    [i_shipaddress] =>
    [i_id] => 229
    )


        [takeAction] => 1
    )

我尝试了一堆代码,例如$ invoice-&gt;数量,$ invoice [1] - &gt;数量,$ this-&gt; $ invoice-&gt;数量,但似乎没有显示任何代码。< / p>

它仍然没有显示,我试着print_r,这就是它给我的数组。

2 个答案:

答案 0 :(得分:2)

所有变量都是private,这意味着您无法从对象外部访问它们。查看发票类的类定义。应该有一些函数可以从对象中获取数量,否则你可以在类中添加这样的特性。

这一点的重点是关注点的分离。该类可能在将来发生变化,可能不会使用相同的结构,因此您应该使用对象函数来访问属性,不要直接将它们作为变量访问。

您可以在the manual或关于面向对象编程的书中阅读有关此主题的更多信息。

答案 1 :(得分:2)

似乎所有数据都在private属性中。您无法直接从外部访问它。

阅读课程文档。应该有一些方法可以调用,例如getQuantity(),它会为您提供数据。这取决于课程的编写方式以及应该如何使用。