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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里