【问题标题】:Merge Multidimensional Array Matches合并多维数组匹配
【发布时间】:2013-07-17 10:27:22
【问题描述】:

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

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 个产品要发送给客户。但是 John Smith 和 Fred Bill 都想要多个项目。与其将数据存储在每个项目的数组中,不如将其设为每个客户的数组。基于订单编号。所以我希望数组最终看起来像这样。

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 拥有自己的行,而是在该数组中的其他产品的行中确定一个字段。所以在一个表格中我们会看到例如:

  • John Smith Product2 Courier(由 product1 的存在确定) 000000000

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

  • John Smith Product3 Courier(由 product1 的存在确定) 000000000
  • John Smith Product4 Courier(由 product1 的存在确定) 000000000
  • John Smith Product5 Courier(由 product1 的存在确定) 000000000

【问题讨论】:

  • 那么您的问题到底是什么?如何合并数组?或者如何产生这些字符串(我不太明白)?你尝试过什么吗?
  • 感谢 @passerby 清理我的烂摊子。

标签: php mysql


【解决方案1】:

你在寻找这样的东西吗?

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

它按 order_id 对所有订单进行分组

【讨论】:

  • 这似乎可以完美地按 order_id 对它们进行分组,非常感谢。关于第二部分的任何想法?下一点是显示客户姓名、项目、电话,然后是必要的产品。但是我需要另一个领域,即方法,即收集或快递。如果其中一个 order_id 数组包含邮资和包装产品,即 Product1,则相应地填写此字段,即 courier 或 collection。所以约翰史密斯将与方法信使一排。拥有 5 个产品和 1 个 P&P 产品的人。他们将在表中有 5 行全部使用 courier 方法
【解决方案2】:

那么,如果我没听错的话,你想在你的 md-array 上应用某种分组吗?

【讨论】:

  • 没错,我想将那些具有相同 order_id 的数组项组合在一起,而不会丢失任何信息。
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多