Can i store array in session php?

Can i store array in session php?
Array can store more items or data in a single variable but these are not available in different pages for use. Any ordinary ( or normal ) array will loose its data as page execution ends. We have to transfer the array each time the visitor moves away from one page to other. In some applications we need to retain the data as long as the visitor is active at site. Shopping cart is the best example for this. Visitor moves between different pages and adds items to the shopping cart. We have to carry the shopping cart to all the pages along with the visitor. Visitor can add or remove items from the cart from any page.

PHP Session variable creating checking and destroying using session_start() using userid and name


Demo shopping cart using session array

We need a session array to retain the data in different pages. Session arrays are like session variables which maintain a unique link between user's web page and the server. You can read more on session management here.

Let us start with declaring a session array. Before that we have to start the session by adding this line at the top of the page
session_start();
Next, any where inside the page we can declare the array (if not done before ) before using. ( Start the shopping cart )
$_SESSION[cart]=array();
Here we have declared a session array. We can add elements to it by array_push() function.
array_push($_SESSION[cart],$prod_id);
We can remove items from the array by using array_diff() function.
$_SESSION[cart]=array_diff($_SESSION[cart],$prod_id);
This way we can add or remove items from out session array. We will use this in our shopping cart script.

Multidimensional Session Array

Can i store array in session php?

We can use multidimensional array to store more than one attributes of an element. We may require to store a product along with the user selection of quantity ( or colour ) in a session array.

Here is how to create multidimensional session array

$_SESSION['cart']=array(array("product"=>"apple","quantity"=>2),
array("product"=>"Orange","quantity"=>4),
array("product"=>"Banana","quantity"=>5),
array("product"=>"Mango","quantity"=>7),
); 

Display elements of the array

$max=sizeof($_SESSION['cart']);
for($i=0; $i<$max; $i++) { 

while (list ($key, $val) = each ($_SESSION['cart'][$i])) { 
echo "$key -> $val ,"; 
} // inner array while loop
echo "
"; } // outer array for loop

Add element to this array

$b=array("product"=>"$product","quantity"=>$quantity);
array_push($_SESSION['cart'],$b); // Items added to cart

Remove elements to this array

unset($_SESSION['cart'][$val1]);// $val1 is the key  of the element
We used above codes in our shopping cart script, you can download and check the outputs
ARRAY REFERENCE Shopping Cart using Session Array

Can i store array in session php?

Can i store array in session php?


This is a tutorial on how to store a PHP array in a session variable. Typically, this sort of design is used on eCommerce websites, where the user is able to add multiple products to their cart. Because the cart is a temporary list, many developers will opt to store it in the user’s session.

Storing an array in a session variable is simple, and it works exactly the same as storing string values or whatnot. Have a look at the following PHP code snippet:

If you run the code above, you’ll see that our session variable contains the $cartArray. The output of our var_dump will look like this:

array (size=3)
  0 => int 123
  1 => int 12
  2 => int 490

Pretty simple, right?

Looping over the session array.

But what if we want to loop through this cart array?

';
    }
}

In the code above:

  • We make sure that the session variable in question actually exists. This is important, as we can never be sure that a particular session variable has been set. If we attempt to iterate over a session array that does not exist, our application will throw out a number of PHP warnings, such as “Warning: Invalid argument supplied for foreach() “
  • We loop through the session array like we would with a regular PHP array. We print out the product ID of the cart item for testing purposes.

To make sure that our session array always exists, we could use the following code:

Here, we check to see if “cart” has been set. If not, we create an empty session array.

Adding elements to the array.

So, what if we want to add items to our session array?

As you can see – our session array works the exact same way as a regular PHP array. If you run and refresh the script above, you’ll see that a new element is added on every page load. This is what my session looked like after multiple refreshes:

array (size=1)
  'cart' => 
    array (size=6)
      0 => int 123
      1 => int 12
      2 => int 490
      3 => int 2787376
      4 => int 2787376
      5 => int 2787376

Hopefully, you found this tutorial to be helpful!

How do you add an array to a session?

We can add elements to it by array_push() function. array_push($_SESSION[cart],$prod_id); We can remove items from the array by using array_diff() function. $_SESSION[cart]=array_diff($_SESSION[cart],$prod_id);

What should I store in session PHP?

A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.

How can store multidimensional array in session in PHP?

This is the basic structure: $product = array(); $product['id'] = $id; $product['type'] = $type; $product['quantity'] = $quantity; And then by using array_push() function I insert that product in SESSION variable.

How can store data in session in PHP?

To use sessions in your script you need to do the following..
Starting a Session. At the beginning of your script, make a call to the session_start() function. ... .
Storing and Accessing Variables. To store variables relevant to the session, assign what you want to a member of the $_SESSION array. ... .
Ending a Session..