2

I am having problem with adding items in $_SESSION variable of PHP with a custom function. so... I want to write sessions with a function like this

public function set($key, $value) {
    $_SESSION[$key] = $value;
}

but every time when I call the function the array is overwritten

//$combination = "1-10"; > 1 = product_id | 10 = option_id

$this->session->set("cart", array(implode("-", $combination) => array(
    "product_id" => $combination["product_id"],
    "variant_id" => $combination["variant_id"],
    "quantity" => 1
)));

OUTPUT

[cart] => Array
    (
        [1-30] => Array
            (
                [product_id] => 1
                [variant_id] => 30
                [quantity] => 1
            )
    )
)

This is working properly

$_SESSION["cart"][implode("-", $combination)] = array(
    "product_id" => $combination["product_id"],
    "variant_id" => $combination["variant_id"],
    "quantity" => 1
);

OUTPUT

[cart] => Array
(
    [1-30] => Array
        (
            [product_id] => 1
            [variant_id] => 30
            [quantity] => 1
        )
    [1-29] => Array
        (
            [product_id] => 1
            [variant_id] => 29
            [quantity] => 1
        )
    [1-28] => Array
        (
            [product_id] => 1
            [variant_id] => 28
            [quantity] => 1
        )
)
2
  • $_SESSION["cart"][implode("-", $combination)] only adds a single array to session[cart]. How do you end up with 3 entries? Commented Mar 13, 2017 at 14:27
  • @apokryfos by adding multiple products in the cart Commented Mar 14, 2017 at 10:03

1 Answer 1

1

You are calling set, which you have written to overwrite the entire array value.

You would either need to get it first, add your value and then set:

$cart  = $this->session->get("cart");
$cart[implode("-", $combination)] = array(
    "product_id" => $combination["product_id"],
    "variant_id" => $combination["variant_id"],
    "quantity" => 1
);
$this->session->set("cart", $cart);

or add a new method to handle a 2 dimensional array:

public function merge($key,$value){
    $_SESSION[$key]=array_merge($_SESSION[$key],$value);
}

$this->session->merge("cart", array(implode("-", $combination) => array(
    "product_id" => $combination["product_id"],
    "variant_id" => $combination["variant_id"],
    "quantity" => 1
)));

or

public function setInArray($key, $subKey, $value){
    $_SESSION[$key][$subKey]=$value;
}

$this->session->setInArray("cart", implode("-", $combination), array(
    "product_id" => $combination["product_id"],
    "variant_id" => $combination["variant_id"],
    "quantity" => 1
));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir! That's what I wanted. Thank you!

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.