简体   繁体   English

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

[英]How do I retrieve an item from an PHP Object Array?

I want to retrieve quantities from this array.我想从这个数组中检索数量。

  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
    )

I've tried a bunch of codes like $invoice->quantity, $invoice[1]->quantity, $this->$invoice->quantity, but none of them seem to display.我尝试了一堆代码,如 $invoice->quantity、$invoice[1]->quantity、$this->$invoice->quantity,但它们似乎都没有显示。

It still does not display at all, I tried to print_r and that is the array it gave me.它仍然根本不显示,我尝试打印_r,这就是它给我的数组。

All the variables are private which means you cannot access them from outside the object.所有变量都是private ,这意味着您无法从对象外部访问它们。 Check out the class definition for the invoice class.查看发票类的类定义。 There should be some function to get the quantities from the object, or else you could add such a feature to the class.应该有一些函数可以从对象中获取数量,否则您可以将这样的功能添加到类中。

The whole point of this is separation of concerns.重点是关注点分离。 The class may change in the future and possibly not use the same structure, therefore you should use object functions to access the properties, do not access them directly as variables.该类将来可能会发生变化,并且可能不会使用相同的结构,因此您应该使用对象函数来访问属性,不要直接将它们作为变量访问。

You can read more on this subject in the manual or in a book about object-oriented programming.您可以在手册或有关面向对象编程的书中阅读有关此主题的更多信息。

Seems like all the data is in a private property.似乎所有数据都在private财产中。 You cannot access it from outside directly.您无法直接从外部访问它。

Read the documentation for the class.阅读该类的文档。 It should have some method you can call, like getQuantity() , that'll get you the data.它应该有一些你可以调用的方法,比如getQuantity() ,它会为你获取数据。 It depends on how the class was written and how it is supposed to be used.这取决于类是如何编写的以及它应该如何使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM