有一个具有以下表达式的数组 $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 收件人规则...
从多维数组中删除重复项
下载声明:
本站所有软件和资料均为软件作者提供或网友推荐发布而来,仅供学习和研究使用,不得用于任何商业用途。如本站不慎侵犯你的版权请联系我,我将及时处理,并撤下相关内容!