duanhuang1699 2019-05-05 23:04
浏览 57
已采纳

如何使用键更改特定多维数组的值?

I'm facing an issue in a multi dimensional array. I need to change the quantity of a specific product set in session if both ID and variants of the selection are the same of one of session product, and increment by 1 this product.

the product I'm posting $newproduct

Array
(
    [id] => 2
    [title] => Vewlix 1080p (red and white)
    [image] => amazing-modern-villa-Freshome-02.jpg
    [variants] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [option_name] => Panel 2 players (+50 euros)
                    [option_price] => 50.00
                )

        )

    [quantity] => 1
    [unit_price] => 1950.00
    [price] => 2000
)

Here is my $_SESSION['shopping_cart']:

Array
(
    [0] => Array
        (
            [id] => 2
            [title] => Vewlix 1080p (red and white)
            [image] => amazing-modern-villa-Freshome-02.jpg
            [variants] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [option_name] => Panel 1 player
                            [option_price] => 0.00
                        )

                )

            [quantity] => 2
            [unit_price] => 1950.00
            [price] => 1950
        )

    [1] => Array
        (
            [id] => 2
            [title] => Vewlix 1080p (red and white)
            [image] => amazing-modern-villa-Freshome-02.jpg
            [variants] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [option_name] => Panel 2 players (+50 euros)
                            [option_price] => 50.00
                        )

                )

            [quantity] => 1
            [unit_price] => 1950.00
            [price] => 2000
        )

    )

The code:

$products_in_cart = array_column($_SESSION["shopping_cart"], "id");
$key = array_search($newproduct["id"], $products_in_cart);

if ($key !== false) {
    $variants_in_cart = array_column($_SESSION["shopping_cart"][$key]["variants"], "id");
    $new_variants = array_column($newproduct["variants"], "id");
    sort($variants_in_cart);
    sort($new_variants);

    if (count(array_diff($variants_in_cart, $new_variants)) === 0) {
        $_SESSION["shopping_cart"][$key]["quantity"] += 1;
    } else {
        $_SESSION["shopping_cart"][] = $newproduct;
    }
} else {
    $_SESSION["shopping_cart"][] = $newproduct;
}

For now I have the ID and variants comparaison working, but when similiar product is posted, instead of incrementing only the specific product with same id/variants, it increments all products quantities of my session by +1.

How can I add +1 only to the product with exact same ID / variants only ? I think my [$key] doesn't work, as it should be the filter to only increment the proper product quantity

  • 写回答

2条回答 默认 最新

  • dongyan8929 2019-05-05 23:37
    关注

    No need to loop over the array like that. Start by getting a list of the IDs in the shopping cart already. If there's a match, pull the key from that list, check the variants, and increment. Otherwise, just add it.

    <?php
    $_SESSION["shopping_cart"] = json_decode('[{"id": 2, "quantity": 1, "variants": [{"option_id": 3}, {"option_id": 9}]}, {"id": 1, "quantity": 1, "variants": [{"option_id": 5}]}]', true);
    $newproduct = json_decode('{"id": 2, "variants": [{"option_id": 3}, {"option_id": 9}]}', true);
    
    $products_in_cart = array_column($_SESSION["shopping_cart"], "id");
    $key = array_search($newproduct["id"], $products_in_cart);
    
    if ($key !== false) {
        $variants_in_cart = array_column($_SESSION["shopping_cart"][$key]["variants"], "option_id");
        $new_variants = array_column($newproduct["variants"], "option_id");
        sort($variants_in_cart);
        sort($new_variants);
        if (count(array_diff($variants_in_cart, $new_variants)) === 0) {
            $_SESSION["shopping_cart"][$key]["quantity"] += 1;
        } else {
            $_SESSION["shopping_cart"][] = $newproduct;
        }
    } else {
        $_SESSION["shopping_cart"][] = $newproduct;
    }
    
    var_dump($_SESSION["shopping_cart"]);
    

    Output:

    array(2) {
      [0] =>
      array(3) {
        'id' =>
        int(2)
        'quantity' =>
        int(2)
        'variants' =>
        array(2) {
          [0] =>
          array(1) {
            ...
          }
          [1] =>
          array(1) {
            ...
          }
        }
      }
      [1] =>
      array(3) {
        'id' =>
        int(1)
        'quantity' =>
        int(1)
        'variants' =>
        array(1) {
          [0] =>
          array(1) {
            ...
          }
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像