boo_lolly Posted February 10, 2009 Share Posted February 10, 2009 Hey guys, I'm custom coding several features for a client using OsCommerce, and I want to have a simple string printed saying how many items are in the cart. This information is handled in the $_SESSION by OsCommerce, so, first, this is what it looks like. rray ( [cart] => shoppingCart Object ( [contents] => Array ( [21] => Array ( [qty] => 1 ) ) [total] => 79.99 [weight] => 7 [cartID] => 87714 [content_type] => ) I thought all I had to do was this: <?php if (empty($_SESSION['cart']['contents'])) { print 'Your cart is empty'; } else { print count($_SESSION['cart']['contents']) .' items in your cart'; } ?> but it brings an error saying I cannot iterate through an Object... But the object is an array... How do I access an Objects attributes if it's embedded into an array? Link to comment https://forums.phpfreaks.com/topic/144697-solved-how-do-i-iterate-through-an-object-array-that-is-embedded-in-another-array/ Share on other sites More sharing options...
gizmola Posted February 10, 2009 Share Posted February 10, 2009 You access object properties using the -> accessor. So something like this: $cart = $_SESSION['cart']; if (count($cart->contents) // empty Link to comment https://forums.phpfreaks.com/topic/144697-solved-how-do-i-iterate-through-an-object-array-that-is-embedded-in-another-array/#findComment-759303 Share on other sites More sharing options...
boo_lolly Posted February 10, 2009 Author Share Posted February 10, 2009 thanks buddy it worked <?php $cart = $_SESSION['cart']; if (count($cart->contents) > 0) { print count($cart->contents) .' item'. ((count($cart->contents) == 1) ? ('') : ('s')) .' in your cart'; } else { print 'Your cart is empty'; } ?> Link to comment https://forums.phpfreaks.com/topic/144697-solved-how-do-i-iterate-through-an-object-array-that-is-embedded-in-another-array/#findComment-759316 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.