我一直與這一整天都在戰鬥,並希望另一組眼睛也許給我一些見解。我不確定我是否正確地接近這個方向。我有一個列出了購買產品的數組,但是如果產品在數組中重複,我想將這些屬性合併在一起。如果數組的值是相同的然後添加到現有的數組
$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