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

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

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) {
        ...
      }
    }
  }
}

Your problem is not a big problem. You might just want to add a correct if to your codes so that to do the ++ for that one new item, that you might get.

I'm not so sure about some of your variables. But, my guesswork is that, you just might need an if before your array_push, maybe something similar to this:

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

and your problem would be solved, if you add a correct if, so that not to add the ++ to all $values. This one also might work:

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