我有一个包含数组结果的会话:
Array (
[0] => Array (
[code] => 16
[qty] => 1
)
[1] => Array (
[code] => 17
[qty] => 1
)
[2] => Array (
[code] => 20
[qty] => 1
)
)
我想删除特定的一个项目,所以我写了代码:
$t = "a";
foreach ($_SESSION["products"] as $cart_itm) //loop through session array var
{
echo "Cart item code".$cart_itm["code"];
echo "product_code".$product_code."<br/>";
if($cart_itm["code"] === $product_code){ //item does,t exist in the list
$t="b";
unset($_SESSION["products"][$product_code]);
}else{
$t = "c";
}
//create a new product list for cart
}
echo "$t";
print_r($_SESSION["products"])
我得到了一个结果:
Cart item code16 product_code17
Cart item code17 product_code17
Cart item code20 product_code17
cArray (
[0] => Array (
[code] => 16
[qty] => 1
)
[1] => Array (
[code] => 17
[qty] => 1
)
[2] => Array (
[code] => 20
[qty] => 1
)
)
我不明白为什么变量不是b因为第二个数组应该返回true?
编辑:
感谢您的帮助,我已经尝试了解决方案,但仍然未设置的会话仍无法正常工作
session_start();
$product_code = $_GET["pid"]; //get the product code to remove
echo "before :";
print_r($_SESSION["products"]);
echo "<br/>";
$t="a";
foreach($_SESSION['products'] as $key => $cart_item) {
if($key === $product_code) {
unset($_SESSION["products"][$key]);
$t="b";
}
}
echo "after :";
print_r($_SESSION["products"]);
echo "$t";
但我仍然有结果:
before:Array([16] =&gt; Array([qty] =&gt; 1)[19] =&gt;数组([qty] =&gt; 1)[18] =&gt;数组([qty] =&gt; 1)) after:Array([16] =&gt; Array([qty] =&gt; 1)[19] =&gt; Array([qty] =&gt; 1)[18] =&gt; Array([qty] =&gt; 1))a
是浏览器缓存的问题吗?
编辑:
我终于找到了解决方案。
$a = "";
foreach ($_SESSION["products"] as $key => $cart_itm) //loop through session array var
{
if($cart_itm["code"] == $product_code) { //item does,t exist in the list
$a = $key;
}
}
$b = (int)$a;
unset($_SESSION["products"][$b]);
答案 0 :(得分:3)
如果您无法管理会话的设置方式,这将对您有用:
foreach($_SESSION['products'] as $key => $cart_itm) {
if($cart_itm["code"] == $product_code){ //item does,t exist in the list
unset($_SESSION["products"][$key]);
}
}
如果我是你,我会像Dagon在评论中所说的那样做。如果您可以访问创建会话变量的PHP,则应将其存储为:
$_SESSION['products'][PRODUCT_CODE] = array('qty' => 1);
这将导致您拥有这样的数组:
Array (
[product_code] => Array (
[qty] => 1
),
[product_code] => Array (
[qty] => 1
),
[product_code] => Array (
[qty] => 1
)
)
这样,你可以简单地运行这样的foreach循环:
foreach($_SESSION['products'] as $key => $cart_item) {
if($key === $product_code) {
unset($_SESSION[$key]);
}
}
正如幽灵所说,这样做会简单得多:
if(isset($_SESSION['products'][$product_code]) unset($_SESSION['products'][$product_code);
或者
if(array_key_exists($product_code, $_SESSION['products'])) {
unset($_SESSION['products'][$product_code]);
}
功能参考:array_key_exists()
&amp; isset()
答案 1 :(得分:0)
首先,变量不是b
的原因是因为您将变量设置回c
分支中的else
。
也就是说,即使变量IS在某个时刻设置为b
,你的循环也会继续运行(到第三次迭代,$cart_itm["code"]
现在是20
并评估else
分支,将变量设置回c
。
其次,你错了 - $product_code
不是正确的索引。
$t = "a";
foreach ($_SESSION["products"] as $key => $cart_itm) //loop through session array var
{
echo "Cart item code".$cart_itm["code"];
echo "product_code".$product_code."<br/>";
if ($cart_itm["code"] === $product_code) { //item does,t exist in the list
$t = "b";
unset($_SESSION["products"][$key]);
}
//create a new product list for cart
}
echo "$t";
print_r($_SESSION["products"]);
你应该得到:
Cart item code16 product_code17
Cart item code17 product_code17
Cart item code20 product_code17
aArray (
[0] => Array (
[code] => 16
[qty] => 1
)
[2] => Array (
[code] => 20
[qty] => 1
)
)