如果删除了[type] => main
,我想取消设置主数组(orders),如果删除了[type] => addon
,则只删除子数组。插件是主要产品在订购时添加的附加项目。如果主要产品被删除主要产品及其插件(主要数组将被删除),如果客户不想要插件,他们可以将其从购物车中删除。
[0] => Array (
[0] => Array (
[name] => Heart Choclates
[code] => LFB-P-10
[qty] => 1
[type] => main
[price] => 1200
[stock] => 1
[image] => choclates-valentines-day.jpg
[quantity] => 12
[expdate] => Jun 02nd 2017
[exptime] => 08:00 AM to 09:00 AM
[expdtype] => Fixed time delivery
)
[1] => Array (
[name] => Birthday Pink
[code] => KB-P-5
[qty] => 1
[type] => addon
[price] => 600
[stock] => 3
[image] => pink-roses.jpg
[expdate] => Jun 02nd 2017
[exptime] => 08:00 AM to 09:00 AM
[expdtype] => Fixed time delivery
)
)
[1] => Array (
[0] => Array (
[name] => Red & Yellow Roses
[code] => KB-P-6
[qty] => 1
[type] => main
[price] => 800
[stock] => 8
[image] => birthday-red-roses.jpg
[expdate] => Jun 15th 2017
[exptime] => 08:00 AM to 12:00 PM
[expdtype] => Standard delivery
)
[1] => Array (
[name] => Truffle Cake
[code] => KB-P-8
[qty] => 1
[type] => addon
[price] => 10
[stock] => 3
[image] => truffle-cake.jpg
[expdate] => Jun 15th 2017
[exptime] => 08:00 AM to 12:00 PM
[expdtype] => Standard delivery
)
)
这就是我的代码的样子。
if (isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"])) {
$product_code = $_GET["removep"]; //get the product code to remove
$product_type = $_GET["removet"]; //get the product type to remove
$return_url = base64_decode($_GET["return_url"]); //get return url
if (isset($_SESSION["grand_total"])) {
unset($_SESSION['grand_total']);
}
$array = $_SESSION["products"];
print_r($array);
foreach($array as $key => $sub_array) {
foreach($sub_array as $innerRow => $cart_itm){
if($cart_itm['type'] == $product_type && $cart_itm['code'] == $product_code) {
unset($array[$key]);
break; //if there will be only one then break out of loop
}
}
}
}
我尝试了这个,但它没有用。谢谢提前
答案 0 :(得分:0)
您需要检查$product_type
是main
还是addon
并进行不同的删除。
foreach($array as $key => $sub_array) {
foreach($sub_array as $innerRow => $cart_itm){
if($cart_itm['type'] == $product_type && $cart_itm['code'] == $product_code) {
if ($product_type == 'main') {
unset($array[$key]);
break; //if there will be only one then break out of loop
} elseif ($product_type == 'addon') {
unset($array[$key][$innerRow]);
}
}
}
}