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?

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.