2017-06-01 82 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 –