3
include('session.php');

$productname = $_GET['productname'];
$productcode = $_GET['productcode'];    

$wishlist = array("$productname" => $productcode);      

$_SESSION["wishlist"] = $wishlist;

print_r($_SESSION["wishlist"]);

This code set as an array to a session named "wishlist".
The problem is that the session is being replaced. I want to add to the array if it already exists.

So how can I update my array with new data. I have tried the following.

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

// CHECK IF SESSION IS EMPTY OR NOT
if(empty($lastsession)) {
    $wishlist = array("$productname" => $productcode);
} else {
    /*
        How Can I Update array ???      
    */
}

The array output is like so. It is associated not numeric indexed. And i want result in single array. Not array in array.

[mobile] => iphone_2

Thank you.

TarangP
  • 2,711
  • 5
  • 20
  • 41

3 Answers3

3

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.

JustCarty
  • 3,839
  • 5
  • 31
  • 51
  • Does [this link](https://stackoverflow.com/a/4649934/3578036) answer your question? It states that the default maximum size a session can be is 128MB – JustCarty Oct 02 '17 at 09:44
  • array_push($wishlist, array("$productname" => $productcode)); this will create a new array in array. i just wants to add value not array so what solution i have? – TarangP Oct 02 '17 at 09:45
  • @JustCarty `array_push` will generate the multidimensional array (-; – Neodan Oct 02 '17 at 09:46
  • I honestly don't understand the problem? You want multiple arrays in $_SESSION i.e. $_SESSION["mobile"]? or do you want the wishlist to be a session and have your wishlist items within that session? – JustCarty Oct 02 '17 at 09:47
  • @TarangPanchal My script does what you want but in more simpler way (-; – Neodan Oct 02 '17 at 09:48
  • yours script works fine but i got this result and i don't wont like this. Array ( [mobile] => iphone_1 [0] => Array ( [mobile] => iphone_2 ) – TarangP Oct 02 '17 at 09:51
  • What would you prefer? What output is it that you'd like and I will work backwards from what you want as opposed to try and create a solution for you. – JustCarty Oct 02 '17 at 09:54
0

Set the wishlist data from the session to variable and then just add the new product to this variable. After that update the wishlist data in the session.

$productname = $_GET['productname'];
$productcode = $_GET['productcode'];

// do the same as: $wishlist = !empty($_SESSION["wishlist"]) ? $_SESSION["wishlist"] : [];
$wishlist = $_SESSION["wishlist"] ?? [];

$wishlist[$productname] = $productcode;
$_SESSION["wishlist"] = $wishlist;

print_r($_SESSION["wishlist"]);
Neodan
  • 5,154
  • 2
  • 27
  • 38
  • $wishlist = $_SESSION["wishlist"] ?? []; Can you please little describe this. – TarangP Oct 02 '17 at 09:31
  • 1
    It's a new PHP7 feature `Null coalescing operator` (http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op) – Neodan Oct 02 '17 at 09:32
0
$_SESSION["wishlist"] = array( 'product1' => 'product1 Name' );
// Initial products in session

$temp_session = $_SESSION["wishlist"];
//store products in wishlist in temp variable

$temp_session['mobile'] = 'iphone_2';
// Add new product to temp variable

$_SESSION["wishlist"] = $temp_session;
//Update session

print_r( $_SESSION["wishlist"] );
shashi
  • 281
  • 1
  • 13
  • you don't think that that is long process when data is large. ? – TarangP Oct 02 '17 at 09:38
  • Yes, defiantly not optimize, but just want to demonstrate how he can update session array – shashi Oct 02 '17 at 09:43
  • Why not just `$_SESSION["wishlist"]["mobile"] = "iphone_2";` That is effectively what you are doing but in multiple steps. After all, $_SESSION is a multi dimensional array. – JustCarty Oct 02 '17 at 09:45