我一直与这一整天都在战斗,并希望另一组眼睛也许给我一些见解。我不确定我是否正确地接近这个方向。我有一个列出了购买产品的数组,但是如果产品在数组中重复,我想将这些属性合并在一起。如果数组的值是相同的然后添加到现有的数组
$order = $order->getProducts();
$newArray = array();
foreach ($order as $order)
{
$product = new Product($order['product_id']);
$combination = new Combination($order['product_attribute_id']);
$attribute = $combination->getAttributesName($context->language->id);
foreach ($attribute as $attribute)
$attributeName = $attribute['name'];
$newArray[] = array(
'id_product' => $order['product_id'],
'name' => $product->name[$context->language->id],
'combination' => array(
'id_product_attribute' => $order['product_attribute_id'],
'type' => $attributeName
)
);
}
我的阵列找出来这样
Array (
[0] => Array
(
[id_product] => 117
[name] => Sidewinder 1-Foot Extension
[combination] => Array
(
[id_product_attribute] => 172
[type] => Black
)
)
[1] => Array
(
[id_product] => 117
[name] => Sidewinder 1-Foot Extension
[combination] => Array
(
[id_product_attribute] => 173
[type] => Black & Yellow
)
)
[2] => Array
(
[id_product] => 119
[name] => ENV100
[combination] => Array
(
[id_product_attribute] => 0
[type] => Black & Yellow
)
)
)
我想尝试实现这一
Array (
[0] => Array
(
[id_product] => 117
[name] => Sidewinder 1-Foot Extension
[combination] => Array
(
[0] => Array
(
[id_product_attribute] => 172
[type] => Black
)
[1] => Array
(
[id_product_attribute] => 173
[type] => Black & Yellow
)
)
)
[1] => Array
(
[id_product] => 119
[name] => ENV100
[combination] => Array
(
[id_product_attribute] => 0
[type] =>
)
)
)
array_merge_recursive – 2014-09-10 20:25:32
将'id_product'存储在某种查找表中 - 关键是'id_product',如果您使用id_product作为数组键,那么值'index' in'newArray' – 2014-09-10 20:25:34
,这会使生活变得更容易,否则您必须循环整个事情每次都要找到要添加的子阵列。 – 2014-09-10 20:25:38