无法访问数组字段PHP

时间:2013-10-25 09:47:02

标签: php arrays object

我将一个包含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 \ fulfillment \ soap \ views \ view.content.php中访问私有属性订单:: $ increment_id

我认为你不能帮助我解决我应该如何访问这些领域的问题?

由于

1 个答案:

答案 0 :(得分:2)

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