我有一个像这样的动态数组,问题看起来很简单,但我没能解决。
[
[
"order_id" => "65",
"products" => [
[
"description" => "Test6",
"brand" => "ACL",
"code" => "303127005576",
"OriginalPrice" => "328.0000"
]
]
],
[
**"order_id" => "66",**
"products" => [
[
"description" => "Test5",
"brand" => "TEA",
"code" => "3900055",
"OriginalPrice" => "43.0000"
]
]
],
[
**"order_id" => "66", **
"products" => [
[
"description" => "Te3st4",
"brand" => "TEA",
"code" => "3900056",
"OriginalPrice" => "43.0000"
]
]
]
];
我想根据 **order_id ** 值合并多维数组内的数组以获得如下结果:
[
[
"order_id" => "65",
"products" => [
[
"description" => "test3",
"brand" => "ACL",
"code" => "303127005576",
"OriginalPrice" => "328.0000"
]
]
],
[
**"order_id" => "66",**
"products" => [
[
"description" => "Test2",
"brand" => "TEA",
"code" => "3900055",
"OriginalPrice" => "43.0000"
],
[
"description" => "Test1",
"brand" => "TEA",
"code" => "3900056",
"OriginalPrice" => "43.0000"
]
]
]
];
我尝试了不同的合并解决方案,但不幸的是对我不起作用
我认为应该有效
$productsTemp = [];
$ordersMerge = [];
foreach ($ordersGlobal as $actualOrder)
{
if (key_exists($actualOrder["order_id"], $productsTemp))
{
$productsTemp[$actualOrder["order_id"]]["products"][] = $actualOrder["products"];
}
else
{
$productsTemp[$actualOrder["order_id"]]["products"] = $actualOrder["products"];
}
}
$ordersMerge[] = $productsTemp;