Jump to content

[SOLVED] How do I iterate through an Object array that is embedded in another array?


boo_lolly

Recommended Posts

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?

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';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.