如何向存储在会话中的关联数组添加值?

3
include('session.php');

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

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

$_SESSION["wishlist"] = $wishlist;

print_r($_SESSION["wishlist"]);

这段代码将一个数组设置为名为“wishlist”的会话。
问题在于会话被替换掉了。如果已经存在,我想要往数组中添加数据。

那么如何使用新数据更新我的数组呢? 我尝试了以下方法。

$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 ???      
    */
}

数组输出如下,与数字索引无关联。

我想要单个数组的结果,而不是嵌套数组。

[mobile] => iphone_2

谢谢您。

3个回答

3
简而言之,如果我理解问题正确,你可以这样做:
$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是一个函数,可以将信息添加到数组的末尾。在这个例子中,我们使用它将产品数组添加到当前的愿望清单中。
另一个简单的解决方案是:
// 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"];

或者用以下代码替换上面代码片段的第5行:

$wishlist[$productname] = $productcode;

这样可以避免像第3行那样创建一个空数组。
array_push相比之下的优点是你可以一次添加多个产品,例如:
$products = [$productname1 => $productcode1, $productname2 => $productcode2];
array_push($wishlist, $products);

我注意到你将会话设置为 $lastsession 并使用 $wishlist。尽量避免重复变量的出现。

这个链接是否回答了你的问题?它指出会话的默认最大大小为128MB。 - JustCarty
array_push($wishlist, array("$productname" => $productcode)); 这将在数组中创建一个新的数组。 我只想添加值而不是数组,那么我有什么解决方案? - TarangP
@TarangPanchal 我的脚本以更简单的方式实现了你想要的功能 (-; - Neodan
你的脚本运行良好,但是我得到了这个结果,我不喜欢这样。 数组 ( [mobile] => iphone_1 [0] => Array ( [mobile] => iphone_2 ) - TarangP
你更喜欢什么?你想要什么样的输出?我会从你想要的结果出发,而不是试图为你创建一个解决方案。 - JustCarty
显示剩余2条评论

0

将愿望清单数据从会话设置为变量,然后只需将新产品添加到此变量中。之后更新会话中的愿望清单数据。

$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"]);

$wishlist = $_SESSION["wishlist"] ?? []; 你能否稍微描述一下这个代码。 - TarangP
1
这是一个新的PHP7功能 Null coalescing operator(http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op) - Neodan

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"] );

当数据量很大时,您不认为这是一个漫长的过程吗? - TarangP
是的,绝对不是优化,只是想演示他如何更新会话数组。 - shashi
为什么不直接使用 $_SESSION["wishlist"]["mobile"] = "iphone_2"; 呢?这样做的效果与您现在的多步操作是一样的。毕竟,$_SESSION 是一个多维数组。 - JustCarty

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接