Array as Session Variable

Array as session variable

Yes, PHP supports arrays as session variables. See this page for an example.

As for your second question: once you set the session variable, it will remain the same until you either change it or unset it. So if the 3rd page doesn't change the session variable, it will stay the same until the 2nd page changes it again.

How to store an array into a session variable in php

Assign

$_SESSION['question'] = $que; 

print_r($_SESSION['question'][0]); will give you first question.

Laravel Store Array in Session

If you need to use the array from session as a string, you need to use Collection like this:

$product = collect([1,2,3,4]);
Session::push('cart', $product);

This will make it work when you will be using {{Session::get('cart');}} in your htmls. Be aware of Session::push because it will append always the new products in sessions. You should be using Session::put to be sure the products will be always updating.

how to change a session array variable value

What's the need of this $val? You could directly update the session value.

if ($_POST['qty_up']=='') {

foreach ($_SESSION["products"] as $key => &$val) {

if ($val["product_code"] == $_POST['code']) {
//$val["product_qty"] += $val["product_qty"];
$_SESSION["products"][$key]['product_qty'] += $val["product_qty"]; // Add this
}

}
}

How to add value to associative array that store in session?

In short, you can do this (if I understand the question correctly):

$productname = $_GET['productname'];
$productcode = $_GET['productcode'];
$lastsession = $_SESSION["wishlist"];

// CHECK IF SESSION IS EMPTY OR NOT
if(empty($lastsession)) {
$wishlist = array("$productname" => $productcode);
} else {
array_push($wishlist, array("$productname" => $productcode));
}

array_push is a function that will add information to the end of an array. In this instance, we are using it to add the product array to the current wishlist.

An alternative simple solution would be:

// create a blank array if the session variable is not created
// array_push requires an array to be passed as the first parameter
$wishlist = isset($_SESSION["wishlist"]) ? $_SESSION["wishlist"] : array();
//$wishlist = $_SESSION["wishlist"] ?? array(); // this is for PHP 7+
array_push($wishlist, array("$productname" => $productcode));

// you can then access each product as:
$wishlist["mobile"];

Or replace line 5 from the above code snippet with the following:

$wishlist[$productname] = $productcode;

This would save you from creating an empty array as in line 3.

The advantage that array_push has over this is that you can add multiple products at once such as:

$products = [$productname1 => $productcode1, $productname2 => $productcode2];
array_push($wishlist, $products);

The one thing I have noticed is that you are setting the session to $lastsession as well as using $wishlist. Try and keep duplicate variables to non-existent.

PHP passing array in session variable

You're setting $_SESSION['userdata'] to the value of $userqty, which is uninitialized.

Change your first line to:

session_start();
$userqty = $_SESSION['userdata']

how to store array of data in Session and retrieve it Laravel?

You will have to use push here.

Session::push('product', $product);

So all new $product push into 'product' session variable.

Casting a Session variable as an array

You need to convert the Session object back to a List

List<string> seatNumFromSession = Session["SeatNum"] as List<string>;

Then you can use it again as you would any other List.

lblseatname.Text = seatNumFromSession[0];


Related Topics



Leave a reply



Submit