在数组PHP中访问变量内部对象

时间:2011-08-30 08:35:38

标签: php oop object

我正在使用

      foreach ($content as $item) {

迭代购物车的一些商品。 我需要从每个项目中获取'type'的值。每个$ item都有一个如下所示的数组:

  'item' => 
    object(stdClass)[6]
      public 'cart_item_id' => string '304' (length=3)
      public 'cart_id' => string '1' (length=1)
      public 'nid' => string '518' (length=3)
      public 'qty' => string '2' (length=1)
      public 'changed' => string '1314686050' (length=10)
      public 'data' => 
        array
          ...
      public 'title' => string 'Info prat om whiskey, inkl. smakprov, 50+ pers.' (length=47)
      public 'vid' => string '409' (length=3)
      public 'cost' => float 0
      public 'price' => float 125
      public 'weight' => int 0
      public 'weight_units' => string 'lb' (length=2)
      public 'module' => string 'uc_product' (length=10)
      public 'model' => string 'GUID-WHISKEY' (length=12)
  'node' => 
    object(stdClass)[42]
      public 'nid' => string '518' (length=3)
      public 'type' => string 'product' (length=7)
      public 'language' => string '' (length=0)
      public 'uid' => string '1' (length=1)
      public 'status' => string '1' (length=1)
      public 'created' => string '1313750365' (length=10)
      public 'changed' => string '1313750365' (length=10)
      public 'comment' => string '0' (length=1)
      public 'promote' => string '0' (length=1)
      public 'moderate' => string '0' (length=1)
      public 'sticky' => string '0' (length=1)
      public 'tnid' => string '0' (length=1)
      public 'translate' => string '0' (length=1)
      public 'vid' => string '409' (length=3)
      public 'revision_uid' => string '1' (length=1)
      public 'title' => string 'Info prat om whiskey, inkl. smakprov, 50+ pers.' (length=47)
      public 'body' => string '' (length=0)
      public 'teaser' => string '' (length=0)
      public 'log' => string '' (length=0)
      public 'revision_timestamp' => string '1313750365' (length=10)
      public 'format' => string '2' (length=1)
      public 'name' => string 'Admin' (length=5)
      public 'picture' => string '' (length=0)
      public 'data' => string 'a:2:{s:13:"form_build_id";s:37:"form-866c69ecddfdbb1158fea2e92629e36c";s:5:"block";a:1:{s:5:"views";a:1:{s:15:"paket_9-block_1";i:1;}}}' (length=135)
      public 'model' => string 'GUID-WHISKEY' (length=12)
      public 'list_price' => string '156.00000' (length=9)
      public 'cost' => string '0.00000' (length=7)
      public 'sell_price' => string '125.00000' (length=9)
      public 'weight' => string '0' (length=1)
      public 'weight_units' => string 'lb' (length=2)
      public 'length' => string '0' (length=1)
      public 'width' => string '0' (length=1)
      public 'height' => string '0' (length=1)
      public 'length_units' => string 'in' (length=2)
      public 'pkg_qty' => string '1' (length=1)
      public 'default_qty' => string '0' (length=1)
      public 'unique_hash' => string '15d33934667fad518c471dc9c20d9ce2' (length=32)
      public 'ordering' => string '0' (length=1)
      public 'shippable' => string '0' (length=1)
      public 'field_en_bild' => 
        array
          ...
      public 'field_image_cache' => 
        array
          ...
      public 'has_body' => string '1' (length=1)
      public 'panel_build_mode_info' => 
        array
          ...
      public 'taxonomy' => 
        array
          ...

如何在'node'中获取'type'的值?我想将它设置在'item'中。我试过了:

      $node->type;
      $node['type'];
      $item['node']->type;

2 个答案:

答案 0 :(得分:2)

从您的错误消息Cannot use object of type stdClass as array 我猜$ item不是数组而是stdClass。

请尝试:$item->node->type

答案 1 :(得分:0)

$node = $item['node'];
$item['item']->type = $node->type;