gpt4 book ai didi

php - 合并多维数组匹配

转载 作者:行者123 更新时间:2023-11-29 08:26:56 24 4
gpt4 key购买 nike

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

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 也是邮政和包装,因此我希望将数据回显到表中。但我不希望“邮政”和“包装”拥有自己的行,而是在该数组中其他产品的行中确定一个字段。因此,在表格中我们会看到例如:

  • John Smith Product2 Courier(根据产品 1 的存在而确定)000000000

如果客户有多种产品,则这需要起作用

  • John Smith Product3 Courier(由产品 1 是否存在确定)000000000
  • John Smith Product4 Courier(由产品 1 是否存在确定)000000000
  • John Smith Product5 Courier(由产品 1 是否存在确定)000000000

最佳答案

你在寻找这样的东西吗?

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

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

关于php - 合并多维数组匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17697318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com