Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

array_push() not working with session in PHP

i am working with PHP, I want to store more than two products in SESSION using array_push(). But problem is that after array_push only 2 products is showing in the cart. When i add more than two product then it is not added into the cart.

Here is my Code:

$dataArray = array();

$cartArray = array(
   $code=>array(
       'id' => $id,
       'name' =>$name,
       'price' =>$price,
       'quantity' =>1)
  );
      
if(empty($_SESSION["shopping_cart"])) {
    $_SESSION["shopping_cart"] = $cartArray;
}
else {
    array_push($dataArray, $_SESSION["shopping_cart"], $cartArray);
    $_SESSION['shopping_cart'] = $dataArray;
}   

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can directly assign values to an array like the below mention.

$_SESSION['shopping_cart'][] = $dataArray;

It will create a 2-d array for "shopping_cart" and every time you add $dataArray

it will store in new key so you can get the "shopping_cart" array having all items

For more about array go throgh this :- php arrays

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading