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条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么