簡體   English   中英

合並多維數組匹配

[英]Merge Multidimensional Array Matches

我正在開發訂單系統,以顯示每個部門需要完成的特定訂單。 我在多維數組中擁有所有必要的數據。 但我想合並任何具有相似值的數組。

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

所以,如果我理解你,你想在你的md陣列上應用某種分組?

你在找這樣的東西嗎?

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

它通過order_id對所有訂單進行分組

這將創建$ target數組的結果。 假設source包含初始數據

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM