合并多维数组匹配

时间:2013-07-17 10:27:23

标签: php mysql

我正在使用我们的订单系统来显示每个部门需要完成的特定订单。我在多维数组中拥有所有必要的数据。但我想合并任何具有相似值的数组。

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,因此我想将数据回显到表中。但我不希望Post和Packaging拥有它自己的行,而是确定该数组中其他产品行中的字段。所以在表格中我们会看到例如:

  • John Smith Product2 Courier(由产品1的存在决定)000000000

如果客户有多个产品,

,则需要这样做
  • John Smith Product3 Courier(由product1确定)000000000
  • John Smith Product4 Courier(由产品1的存在决定)000000000
  • John Smith Product5 Courier(由产品1的存在决定)000000000

3 个答案:

答案 0 :(得分:1)

所以,如果我理解你,你想在你的md阵列上应用某种分组吗?

答案 1 :(得分:1)

你找这样的东西吗?

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

它通过order_id

对所有订单进行分组

答案 2 :(得分:0)

这会将结果创建为$ target数组。假设source包含初始数据

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