1

I got following array in my PHP $_SESSION

[CART] => SHOPPINGCART OBJECT
       (
             [CONTENTS] => ARRAY
                  (
                     [121] => ARRAY
                        (
                            [QTY] => 1
                         )

I know how to change some simple $_SESSION variable, but what if there is an array in array and then there is the value I want to change? Or what if i wanted to add a new Array to [CONTENTS]? Sadly i couldn't find a solution on here.

4
  • What exactly do you want to change in the array? Commented Dec 10, 2015 at 9:54
  • In this case for example I want to change the QTY from 1 to 5 Commented Dec 10, 2015 at 9:55
  • @monace19 You might want to rethink that based on [CART] => SHOPPINGCART OBJECT Commented Dec 10, 2015 at 9:57
  • @monace19 Nope, not working. And it is possible that there are 2 or 3 QTY in session, but only one which belongs to [121] so it has to be specified somehow. Commented Dec 10, 2015 at 9:57

3 Answers 3

2

Your $_SESSION array actually contains an OBJECT which contains and ARRAY which contains an ARRAY

So using normal notation to access QTY for example you would do

echo $_SESSION['CART']->CONTENTS[121]['QTY'];

Or changing the QTY

$_SESSION['CART']->CONTENTS[121]['QTY'] = 10;

Adding a new array to CONTENTS would be

$_SESSION['CART']->CONTENTS[] = array('QTY' => 2);

Then viewing all the CONTENTS array you could do

foreach ( $_SESSION['CART']->CONTENTS as $id => $content ) {
    echo "$id\n"
    foreach ( $content as $qty ) {
        echo "    $qty\n";
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

This gets me a Parse Error: PARSE ERROR(4): "syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'"
Sorry missed the 121 occurance, see amended example
2 seconds I will test
@MarcelWasilewski use this $_SESSION['CART'] instead $_SESSION->['CART']
@MarcelWasilewski Thats it, got a bit confused there for a second. Thanks for spotting the typo
|
0

One could use a combination of serialize()/unserialize().
To store it in a session:

$cart= new SHOPPINGCART();
$_SESSION['cart'] = serialize($cart);

To actually change values from it:

$cart = unserialize($_SESSION['cart']);
// do anything with cart and store it afterwards with the code above
$cart["quantity"] += 1; // increment it by one
$_SESSION['cart'] = serialize($cart);

1 Comment

He is not asking how to store it, he is asking how to access what is already stored, presumably by something he has not written himself
0
$temp = $_SESSION['CART'];
$temp->CONTENTS['121']['QTY'] = 5;
$_SESSION['CART'] = $temp;

Check if this works; then we can think of a way for specifying "121".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.