July 24, 2013

How to access info stored in session array?

JL.’s Question:

Given the following code:

session_start();

$cart = $_SESSION['cart'];

print_r($_SESSION['cart']);

I can then see what I want to access:

Array ( [153c740f526f2fa8aac9e1ddfdce2716] => Array ( [deal_id] => 38 [variation_id] => [variation] => [quantity] => 6 [data] =>……

There is still more but that is the basics…

What I want to be able to do is get and set the quantity:

So I’ve tried:

$cart = $_SESSION['cart'];

for ($i = 0 ; $i < count($cart) ; $i ++)
{
    echo "The session variable you want" . $_SESSION['cart'][$i]['deal_id'];
    echo "<br>";

}

But there is no output, what am I doing wrong?

You will simplify things by using foreach loop

foreach ($_SESSION['cart'] as $k => $data) {
    echo "The session variable you want" . $data['deal_id'];
    echo "<br>";
    $_SESSION['cart'][$k] = "new Value";
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!