【问题标题】:How do I retrieve an item from an PHP Object Array?如何从 PHP 对象数组中检索项目?
【发布时间】:2012-09-02 18:06:45
【问题描述】:

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

  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,但似乎都没有显示。

它仍然根本不显示,我尝试 print_r 并且那是它给我的数组。

【问题讨论】:

  • 你有多个数组。你要哪一个?
  • 看起来 everything 都封装在 $data 属性下。有趣...
  • 它在一个函数中 public function onPurchaseGenerated( $member, $package, $invoice, $purchase ) {

标签: php arrays object


【解决方案1】:

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

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

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

【讨论】:

  • 通过公共函数 onPurchaseGenerated( $member, $package, $invoice, $purchase ) { 它在一个函数内
  • 发票显然是它自己的类。您不能从其他类访问私有属性。
【解决方案2】:

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

阅读该类的文档。它应该有一些您可以调用的方法,例如getQuantity(),它将为您获取数据。这取决于类是如何编写的以及它应该如何使用。

【讨论】:

    猜你喜欢
    • 2012-09-02
    • 2013-06-22
    • 1970-01-01
    • 2016-02-24
    • 2011-07-07
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    相关资源
    最近更新 更多