如何从 PHP 对象数组中检索项目? [英] How do I retrieve an item from an PHP Object Array?

查看:42
本文介绍了如何从 PHP 对象数组中检索项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
    )

我尝试了一堆代码,例如 $invoice->quantity、$invoice[1]->quantity、$this->$invoice->quantity,但似乎都没有显示.

I've tried a bunch of codes like $invoice->quantity, $invoice[1]->quantity, $this->$invoice->quantity, but none of them seem to display.

它仍然根本不显示,我尝试打印_r,这就是它给我的数组.

It still does not display at all, I tried to print_r and that is the array it gave me.

推荐答案

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

All the variables are private which means you cannot access them from outside the object. 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.

这篇关于如何从 PHP 对象数组中检索项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆