2017-06-01 76 views
1

如果删除了任何[type] => main,我想取消设置主数组(订单),并且如果删除[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

你需要检查$product_type是否mainaddon和做不同的缺失并没有work.Thanks。

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

DEMO

+0

我想这已经但是它没有得到取消设置 –

+0

我添加了一个演示展示它的工作原理。 – Barmar

+0

是的,它工作的错误是我没有将数组保存到会话。 $ _SESSION [“products”] = $ array;喜欢这个。所以它没有反映在我的购物车上。谢谢你soo –