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

时间:2019-05-05 23:04:40

标签: php multidimensional-array foreach

我正面临多维数组中的一个问题。如果选择的ID和变体都与会话产品之一相同,则我需要更改会话中特定产品集的数量,然后将此产品加1。

我要发布的产品$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
)

这是我的$_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
        )

    )

代码:

$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;
}

现在我可以使用ID和变体比较,但是在发布相似产品时,与其仅增加具有相同ID /变体的特定产品,不但将我会话中的所有产品数量增加+1。

如何仅向具有完全相同ID /变体的产品添加+1?我认为我的[$ key]不起作用,因为它应该是仅增加适当产品数量的过滤器

2 个答案:

答案 0 :(得分:1)

不需要像这样遍历数组。首先获取购物车中的ID列表。如果有匹配项,请从该列表中拔出密钥,检查变体并递增。否则,只需添加它即可。

<?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"]);

输出:

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 :(得分:0)

您的问题不是大问题。您可能只想在代码中添加正确的if,以便为您可能会得到的那个新项目做++

我不太确定您的某些变量。但是,我的猜测是,您可能只需要if之前就需要一个array_push,也许与此类似:

        if ((int) $newproduct['id'] == (int) $value["id"]) {
            $_SESSION['shopping_cart'][$key]['quantity'] = (int) $_SESSION['shopping_cart'][$key]['quantity'] + 1;
        }

,如果您添加正确的if,从而不会将++添加到所有$value,则会解决您的问题。这也可能起作用:

        if ((int) $newproduct['id'] == (int) $value["id"]) {
            $_SESSION['shopping_cart'][$key]['quantity'] = (int) $value['quantity'] + 1;
        }

$_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
            (
                "option_id" => "1",
                "option_name" => "Panel 1 player",
                "option_price" => "0.00",
            ),

        ),

        "quantity" => "1",
        "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
            (
                "option_id" => "1",
                "option_name" => "Panel 2 players (+50 euros)",
                "option_price" => "50.00",
            ),

        ),

        "quantity" => "1",
        "unit_price" => "1950.00",
        "price" => "2000",
    ),

);

foreach ($_SESSION['shopping_cart'] as $key => $value) {
    if (in_array($newproduct['id'], $shopping_ids) && empty($variants_diff)) {
        if ((int) $newproduct['id'] == (int) $value["id"]) {
            $_SESSION['shopping_cart'][$key]['quantity'] = (int) $_SESSION['shopping_cart'][$key]['quantity'] + 1;
        }
    } else {
        array_push($_SESSION['shopping_cart'], $newproduct);
    }
}