2014-09-10 61 views
0

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

$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] => 
      ) 

    ) 

) 
+1

array_merge_recursive – 2014-09-10 20:25:32

+0

將'id_product'存儲在某種查找表中 - 關鍵是'id_product',如果您使用id_product作爲數組鍵,那麼值'index' in'newArray' – 2014-09-10 20:25:34

+0

,這會使生活變得更容易,否則您必須循環整個事情每次都要找到要添加的子陣列。 – 2014-09-10 20:25:38

回答

0

也許能夠使它看起來更好一點,但這應該工作:

foreach ($order as $order) { 
    $product = new Product($order['product_id']); 
    $combination = new Combination($order['product_attribute_id']); 
    $attribute = $combination->getAttributesName($context->language->id); 

    $newArray[$order['product_id']] = array(
      'id_product' => $order['product_id'], 
      'name' => $product->name[$context->language->id], 
    ); 

    foreach ($attribute as $attr) {   
     $newArray[$order['product_id']]['combination'][] => array(
       'id_product_attribute' => $attr['id'], // a guess 
       'type' => $attr['name'], 
     ); 
    } 
} 

個數組鍵將是product_id所以如果你想讓他們爲你展示,然後在循環之後:

$newArray = array_values($newArray); 

我帶着刺不知道這是什麼意思做:

foreach ($attribute as $attribute) 
     $attributeName = $attribute['name']; 
+0

核心函數組合 - > getAttributesName返回一個多維數組,我用它來訪問該名稱。你寫的這部分確實可以工作,但它不會添加到組合數組中。只需在陣列中添加最後一個,就可以輸入 – 2014-09-10 20:40:49

+0

上次編輯。注意''id_product_attribute''將需要來自'$ attribute'數組而不是'$ order'。最好的我沒有所有的數組結構。 – AbraCadaver 2014-09-10 20:50:59

+0

它像一個魅力工作,我感謝幫助! – 2014-09-10 20:51:43

相關問題