1

I am using a

      foreach ($content as $item) {

to iterate through some items for a shopping cart. I need to get the value of 'type' from each of these items. Each $item has an array that looks like this:

  '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
          ...

How can I get the value of 'type' inside 'node'? I would like to set it inside 'item'. I have tried:

      $node->type;
      $node['type'];
      $item['node']->type;
2
  • $item['node']->type; should work, what is the error msg? Commented Aug 30, 2011 at 8:47
  • it says Cannot use object of type stdClass as array. BUT maybe I'm explaining it wrong. $item is an array, those are what I'm iterating through. Inside that array are $item and $node, which are both objects. Commented Aug 30, 2011 at 9:06

2 Answers 2

2

From your error msg Cannot use object of type stdClass as array I guess the $item is not array but stdClass.

So try: $item->node->type.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.