Php 如何使用该键更改特定多维数组的值?
我在多维数组中面临一个问题。我需要在会话中更改特定产品集的数量,如果选择的ID和变体与会话产品的ID和变体相同,并且该产品的增量为1 我正在发布的产品Php 如何使用该键更改特定多维数组的值?,php,multidimensional-array,foreach,Php,Multidimensional Array,Foreach,我在多维数组中面临一个问题。我需要在会话中更改特定产品集的数量,如果选择的ID和变体与会话产品的ID和变体相同,并且该产品的增量为1 我正在发布的产品$newproduct Array ( [id] => 2 [title] => Vewlix 1080p (red and white) [image] => amazing-modern-villa-Freshome-02.jpg [variants] => Array (
$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
)
这是我的$\u会话['shopping\u 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]不起作用,因为它应该是只增加适当产品数量的过滤器你的问题不是什么大问题。您可能只想在代码中添加一个正确的
if
,以便对一个新项目执行++
,您可能会得到
我不太确定你的一些变量。但是,我的猜测是,在推送数组之前,您可能需要一个if
,可能类似于:
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;
}
不需要像那样在阵列上循环。从已经在购物车中获取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"]);
什么是$newproduct
?只是另一个与会话中相同的数组?$newproduct
是我在我的$\u会话['shopping\u cart']
中推动的,如果ID不同,或者ID相同,但我的产品的变体不同。嗨,Emma!同样的行为。它为我购物盒中的所有产品增加+1个数量,如果我不玩这些变体,它会起作用。但是我如何检查每个产品中的不同变体,并且仅在ID相同(我们现在有)但变体也相同的情况下添加+1?添加push$newproduct
如果两者中有一个不同,则在增量几乎有效的同一代码块中为该数组添加类似的检查。我创建了这个数组$variants\u diff=array\u map('unserialize',array\u diff(array\u map('serialize',$variants\u choices)),array\u map('serialize',$shopping\u variants))在比较变体之前,使用foreach编码>。它是有效的。如果变量相似,或者返回值在变量中发生变化,则我有一个或一个空数组()。除了找到两个具有相同ID但不同变体的产品外,所有工作都正常,第一个产品是+1(我再次不知道为什么[$key]似乎不工作。我的主题更新中的代码我不知道,我的代码查看变体并在这两种情况下都正常工作。也许你应该使用它。我使用了,$products\u variants\u在\u cart=array\u列中($_SESSION[“shopping_cart”],“variants”);$result=array_search($newproduct['variants',$products_variants_in_cart);
但无法使其工作
<?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) {
...
}
}
}
}