嘿,伙计们,我有这个数组:
Array
(
[qty] => 1
[id] => 2
[name] => sallate me gjera plot
[price] => 8
[category_id] => 25
[dish_name] => sallate me gjera plot
[dish_id] => 2
[dish_category_id] => 25
[dish_qty] => 1
[dish_price] => 8
)
Array
(
[qty] => 1
[id] => 1
[name] => sallate cezar
[price] => 12
[category_id] => 25
[dish_name] => sallate cezar
[dish_id] => 1
[dish_category_id] => 25
[dish_qty] => 1
[dish_price] => 12
)
我想做的是通过dish_id取消设置项目。我打算这样做:
if(isset($_SESSION["cart_products"]) && count($_SESSION["cart_products"])>0)
{
foreach ($_SESSION["cart_products"] as $key =>$cart_itm)
{
if($cart_itm["dish_id"]==$removable_id)
{
unset($cart_itm[$key]);
}
}
}
谁能告诉我我现在在做什么吗。。感谢:D
实际上,您需要从实际数组中unset
数据,该数组是$_SESSION["cart_products"]
而不是$cart_itm
。
因此,将unset($cart_itm[$key]);
更改为unset($_SESSION["cart_products"][$key])
作为替代方案,您可以使用带有匿名函数的array_filter()
:
$removable_id = 1;
$_SESSION["cart_products"] = array_filter
(
$_SESSION["cart_products"],
function( $row ) use( $removable_id )
{
return $row['dish_id'] != $removable_id;
}
);
print_r( $_SESSION["cart_products"] );
将打印:
Array
(
[0] => Array
(
[qty] => 1
[id] => 2
[name] => sallate me gjera plot
[price] => 8
[category_id] => 25
[dish_name] => sallate me gjera plot
[dish_id] => 2
[dish_category_id] => 25
[dish_qty] => 1
[dish_price] => 8
)
)