2013-07-17 88 views
0

我正在处理我们的订单系统以显示需要按部门完成的特定订单。我有一个多维数组中的所有必要数据。但我想合并任何具有相似值的数组。合并多维数组匹配

Array 
(
    [0] => Array 
    (
     [id] => 16111 
     [order_id] => 1234 
     [item_id] => Product 1 
     [invoice_id] => 98765 
     [acc_id] => 1 
     [name] => John Smith 
     [phone] => 000000000 
    ) 

    [1] => Array 
    (
     [id] => 16112 
     [order_id] => 1234 
     [item_id] => Product 2 
     [invoice_id] => 98765 
     [acc_id] => 1 
     [name] => John Smith 
     [phone] => 000000000 
    ) 

    [2] => Array 
    (
     [id] => 16113 
     [order_id] => 1235 
     [item_id] => Product 3 
     [invoice_id] => 98721 
     [acc_id] => 11 
     [name] => Bob Jones 
     [phone] => 222222222 
    ) 

    [3] => Array 
    (
     [id] => 16114 
     [order_id] => 1236 
     [item_id] => Product 4 
     [invoice_id] => 98754 
     [acc_id] => 3 
     [name] => Fred Bill 
     [phone] => 111111111 
    ) 

    [4] => Array 
    (
     [id] => 16115 
     [order_id] => 1236 
     [item_id] => Product 1 
     [invoice_id] => 98754 
     [acc_id] => 3 
     [name] => Fred Bill 
     [phone] => 111111111 
    ) 
) 

我们可以看到我们有5种产品发送给客户。但约翰史密斯和弗雷德比尔都想要多个项目。我不希望将数据存储在每个项目的数组中,而宁愿将其作为每个客户的数组。根据订单ID。所以我希望数组最终看起来像这样。

Array 
    (
    [0] => Array 
    (
     [0] => Array 
      (
       [id] => 16111 
       [order_id] => 1234 
       [item_id] => Product 1 
       [invoice_id] => 98765 
       [acc_id] => 1 
       [name] => John Smith 
       [phone] => 000000000 
      ) 
     [1] => Array 
      (
       [id] => 16112 
       [order_id] => 1234 
       [item_id] => Product 2 
       [invoice_id] => 98765 
       [acc_id] => 1 
       [name] => John Smith 
       [phone] => 000000000 
      ) 
    ) 

    [1] => Array 
    (
     [id] => 16113 
     [order_id] => 1235 
     [item_id] => Product 3 
     [invoice_id] => 98721 
     [acc_id] => 11 
     [name] => Bob Jones 
     [phone] => 222222222 
    ) 

    [2] => Array 
    (
     [0] => Array 
      (

       [id] => 16114 
       [order_id] => 1236 
       [item_id] => Product 4 
       [invoice_id] => 98754 
       [acc_id] => 3 
       [name] => Fred Bill 
       [phone] => 111111111 

      ) 
     [1] => Array 
      (
       [id] => 16115 
       [order_id] => 1236 
       [item_id] => Product 1 
       [invoice_id] => 98754 
       [acc_id] => 3 
       [name] => Fred Bill 
       [phone] => 111111111 
      ) 



    ) 

) 

产品1也是邮政和包装,所以我想然后将数据回显到表中。但我不希望Post和Packaging具有自己的行,而是确定此阵列中其他产品的行中的字段。因此,在一个表中,我们会看到,例如:

  • 约翰·史密斯产品2速递(由产品1的存在与否决定)000000000

这需要工作,如果客户拥有多个产品,即

  • 约翰史密斯产品3信使(由产品1的存在决定)000000000
  • 约翰史密斯产品4信使(由产品1的存在决定)000000000
  • 约翰·史密斯产品5速递(由产品1的存在与否决定)000000000
+0

那么你的问题究竟是什么?如何合并数组?或如何产生这些字符串(我不太明白)?并尝试一些东西? – Passerby

+0

谢谢@passerby清理我的混乱。 –

回答

1

你是否在寻找类似的东西?

foreach($array as $order) 
{ 
    $orders[$order['order_id']][] = $order; 
} 

它把所有的订单由ORDER_ID

+0

这似乎很好地将它们按order_id分组,非常感谢。第二部分有什么想法?接下来的一点是显示客户名称,物品,电话,然后是必要的产品。但是,我需要另一个领域是方法,即收集或快递。如果其中一个order_id数组包含邮资和包装产品,即Product1,则相应填写该字段,即快递或收款。因此,约翰史密斯将与方法快递有一行。有5种产品和1种P&P产品的人。他们将在表中使用方法快递5行 –

1

所以,如果我理解你的权利,你想申请某种您的MD阵列上分组的?

+0

这是正确的,我想将具有相同order_id的数组项目组合在一起,而不会丢失任何信息。 –

0

这将创建结果作为$目标阵列。假设源包含初始数据

foreach ($source as $src) { 
    $acct_id = $src['acc_id']; 
    $target[$acct_id][] = $src; 
}