Ok I need to work out how to do I get the following to work.

I have an array with sub-array

array (
  'session' => '1359964785.85203874781',
  'status' => 'cart',
  'items' => 
  array (
    'id' => '510bca8138fc5d6e38000000',
    'quantity' => '1',
  ),
)

then when I put that array thought my foreach

<?php
       $cart = Shop::get_cart();
       ;
            if($cart != NULL)
            { 
                foreach($cart[0]['items'] as $items)
                {
                    print $items['id'];
                ?>
                <tr>

                </tr>
                <?php
                }   
            }
       ?>

I cant seem to access the sub array for items

what I want to be able to do is call it like

$items["id"];
$items["quantity"];

EDIT

I am getting the data from MongoDB by the following code

public function get_cart()
    {
        $collection = static::db()->redi_shop_orders;
        $cursor = $collection->find(array('session' => $_SESSION["redi-Shop"]));
        if ($cursor->count() > 0)
        {
            $posts = array();
            // iterate through the results
            while( $cursor->hasNext() ) {   
                $posts[] = ($cursor->getNext());
            }
            return $posts;
        }
    }

which when I do a print out returns

Array ( [0] => Array ( [_id] => MongoId Object ( [$id] => 510f8eba38fc5d7d43000000 ) [session] => 1359964785.85203874781 [status] => cart [items] => Array ( [id] => 510bca8138fc5d6e38000000 [quantity] => 1 ) ) )

can someone please help

share|improve this question
1  
is_array($cart)? isset($cart[0])? isset($cart[0]['items'])? is_array($cart[0]['items'])? - There's some really basic debugging you can do on your own. – Leigh Feb 4 at 11:13

2 Answers

YOu should replace

foreach($cart[0]['items'] as $items)

with

foreach($cart['items'] as $items)
share|improve this answer
that did not work – RussellHarrower Feb 4 at 12:12
What does not work ? – Baba Feb 4 at 12:34
foreach($cart['items'] as $items) – RussellHarrower Feb 4 at 12:49
var_dump($cart) what do you have ? – Baba Feb 4 at 14:16
<?php
   $cart = array (
  'session' => '1359964785.85203874781',
  'status' => 'cart',
  'items' => 
  array (
 'id' => '510bca8138fc5d6e38000000',
'quantity' => '1',
 ),
);

        if($cart != NULL)
        { 
            foreach($cart['items'] as $items)
            {
                print $items['id'];
            ?>
            <tr>

            </tr>
            <?php
            }   
        }
   ?>
share|improve this answer
Could you explain what went wrong and how you fixed it? That way it will be clear very fast to everyone. – Rune Feb 4 at 11:41
That still does not work, Maybe its because I use MongoDB? – RussellHarrower Feb 4 at 12:06

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.