0

我将一个包含 3 个对象的数组返回给我只想访问第 3 层数据的函数。

现在,当返回时,我可以 var_dump 整个数组并且我可以看到它,我还可以 var_dump 数组加上我想使用的对象的编号并查看它。下面的一个例子:

我使用这段代码:

$data = Container::make_orderList();
    var_dump($data);

我得到了这个结果:

array (size=3)
0 => 
object(order)[63]
  private 'increment_id' => string '100000002' (length=9)
  private 'created_date' => string '2013-10-19 19:34:02' (length=19)
  private 'is_active' => null
  private 'weight' => string '20.0000' (length=7)
  private 'status' => string 'processing' (length=10)
1 => 
object(address)[55]
  private 'company_name' => string 'No Company' (length=10)
  private 'street' => string '20 Waterfall Way 
  Barwell' (length=25)
  private 'city' => string 'Leicester' (length=9)
  private 'region' => string 'Leicestershire' (length=14)
  private 'postcode' => string 'LE9 8EH' (length=7)
2 => 
object(address)[54]
  private 'company_name' => string 'CRanbri Web Solutions' (length=21)
  private 'street' => string '4 Turner Drive
  Hinckley' (length=23)
  private 'city' => string 'Leciester' (length=9)
  private 'region' => string 'LEicesterhsire' (length=14)
  private 'postcode' => string 'LE10 0gu' (length=8)
  private 'country_id' => string 'GB' (length=2)

与:

$data = Container::make_orderList();
    var_dump($data[0]);

我得到这个结果:

object(order)[63]
 private 'increment_id' => string '100000002' (length=9)
 private 'created_date' => string '2013-10-19 19:34:02' (length=19)
 private 'is_active' => null
 private 'weight' => string '20.0000' (length=7)
 private 'status' => string 'processing' (length=10)
 private 'shipping_address_id' => string '4' (length=1)
 private 'billing_address_id' => string '3' (length=1)
 private 'shipping_method' => string 'flatrate_flatrate' (length=17)
 private 'shipping_description' => string 'Flat Rate - Fixed' (length=17)
 private 'order_id' => string '2' (length=1)
 private 'gift_message' => null

这很棒,也是我需要的,但我遇到的麻烦是访问数组的各个字段,例如“increment_id”字段。

我努力了:

$data = Container::make_orderList();
    var_dump($data[0]->increment_id);

但我得到这个错误:

致命错误:无法在第 20 行访问 C:\xampp\htdocs\magento_soap_client\fulfilment\soap\views\view.content.php 中的私有财产 order::$increment_id

我不认为您可以帮助我说明我应该如何访问这些字段?

谢谢

4

1 回答 1

2

只需将对象的字段公开或为这些字段创建 getter 和/或 setter。您可能想查看有关PHP 中属性和方法的可见性的文档。

于 2013-10-25T09:49:43.560 回答