【发布时间】:2021-06-19 05:40:53
【问题描述】:
我有一个名为 $products_list 的数组,其中填充了数据,结构如下:
$products_list[] = [
'manufacturer' => $manufacturer_name,
'mpn' => $mpn,
'description' => $description,
'quantity' => $quantity,
'condition' => $condition,
'price' => $price
];
如果它们具有相同的描述 && 条件,我会尝试从数组中删除任何重复值,并合并重复项中的数量。
我试图将数据设置为一个临时数组,以便能够将两个数组比较到一个 foreach 语句中。像这样:
foreach($products_list as $key => $row) {
if($test_data['description'] == $row['description'] && $test_data['isbn'] == $isbn)
{
echo $row['description'];
echo $row['quantity']+$temp_quantity;
}
$test_data = [
'description' => $row['description'],
'isbn' => $row['isbn'],
'quantity' =>
];
}
【问题讨论】:
-
你能分享一下你的尝试吗?
-
1.创建 new 空数组 2. 迭代 current 数组。 3. 检查 current 数组中的元素是否已经存在于 new 之一中 - 如果不添加,如果存在,则修改数量。
标签: php arrays duplicates