有一个具有以下表达式的数组 $products[] = $product[$category_id][$product_id]:$products = array( 33 => array( 31 => array( 'model' => 'Product 4', ...
有一个具有以下表达式的数组 $products[] = $product[$category_id][$product_id]
:
$products = array(
33 => array(
31 => array(
'model' => 'Product 4',
'product_id' => 31,
'sku' => '',
'ean' => '1234',
'price' => '80.0000'
),
8733 => array(
'model' => 'qqq',
'product_id' => 8733,
'sku' => '',
'ean' => '1000',
'price' => '344.6281'
)
),
25 => array(
30 => array(
'model' => 'Product 3',
'product_id' => 30,
'sku' => '',
'ean' => '250',
'price' => '50.4132'
),
31 => array(
'model' => 'Product 4',
'product_id' => 31,
'sku' => '',
'ean' => '1234',
'price' => '80.0000'
)
)
);
我需要在 php 中删除重复的产品,结果必须是:
$products = array(
33 => array(
8733 => array(
'model' => 'qqq',
'product_id' => 8733,
'sku' => '',
'ean' => '1000',
'price' => '344.6281'
)
),
25 => array(
30 => array(
'model' => 'Product 3',
'product_id' => 30,
'sku' => '',
'ean' => '250',
'price' => '50.4132'
),
31 => array(
'model' => 'Product 4',
'product_id' => 31,
'sku' => '',
'ean' => '1234',
'price' => '80.0000'
),
)
);
我尝试过这个:
$cleared_products = array();
foreach ($products as $category_id => $category_products) {
foreach ($category_products as $product_id => $product) {
if (isset($cleared_products[$category_id][$product_id])) {
unset($products[$category_id][$product_id]);
} else {
$cleared_products[$category_id][$product_id] = $product;
}
}
}
但不起作用......
编辑以解释为什么我需要 生成 XML 并且任何产品都不能重复。它们属于不同的类别并不重要。XML 收件人规则...
解决方案
$products = array(
33 => array(
31 => array(
'model' => 'Product 4',
'product_id' => 31,
'sku' => '',
'ean' => '1234',
'price' => '80.0000'
),
8733 => array(
'model' => 'qqq',
'product_id' => 8733,
'sku' => '',
'ean' => '1000',
'price' => '344.6281'
)
),
25 => array(
30 => array(
'model' => 'Product 3',
'product_id' => 30,
'sku' => '',
'ean' => '250',
'price' => '50.4132'
),
31 => array(
'model' => 'Product 4',
'product_id' => 31,
'sku' => '',
'ean' => '1234',
'price' => '80.0000'
)
)
);
// Create a new array to store unique products
$uniqueProducts = array();
foreach ($products as $parentKey => $items) {
foreach ($items as $productId => $product) {
$found = false;
// Check if the product already exists in the uniqueProducts array
foreach ($uniqueProducts as $existingItems) {
if (isset($existingItems[$productId]) && $existingItems[$productId] == $product) {
$found = true;
break;
}
}
// If the product is not found, add it to the uniqueProducts array
if (!$found) {
$uniqueProducts[$parentKey][$productId] = $product;
}
}
}
// Output the unique products array
print_r($uniqueProducts);
输出 :
Array
(
[33] => Array
(
[31] => Array
(
[model] => Product 4
[product_id] => 31
[sku] =>
[ean] => 1234
[price] => 80.0000
)
[8733] => Array
(
[model] => qqq
[product_id] => 8733
[sku] =>
[ean] => 1000
[price] => 344.6281
)
)
[25] => Array
(
[30] => Array
(
[model] => Product 3
[product_id] => 30
[sku] =>
[ean] => 250
[price] => 50.4132
)
)
)
在线处决 : 3v4l
解释 :
$uniqueProducts
用于存储没有重复的产品。
$products
数组中的每个产品。
$uniqueProducts
已经存在 product_id
。
$uniqueProducts
.