[英]Grouping data using a value from a deep subarray
How can I assign first level keys using a column value from the third level of a multidimensional array?如何使用多维数组第三级的列值分配第一级键?
My input:我的输入:
$array = [
[
["ID" => 2, "vendor_id" => "37", "order_id" => 776],
],
[
["ID" => 2, "vendor_id" => "37", "order_id" => 786],
]
];
My current output is like this:我目前的输出是这样的:
array(1) {
[787]=>
array(2) {
[0]=>
array(40) {
["ID"]=>
string(1) "1"
["vendor_id"]=>
string(2) "37"
["order_id"]=>
string(3) "776"
}
[1]=>
array(40) {
["ID"]=>
string(1) "2"
["vendor_id"]=>
string(2) "37"
["order_id"]=>
string(3) "787"
}
}
}
I want to group the value of order_id
separately as a key - the end result would look like this:我想将order_id
的值单独分组为一个键 - 最终结果如下所示:
array(1) {
[776]=>
array(2) {
[0]=>
array(40) {
["ID"]=>
string(1) "2"
["vendor_id"]=>
string(2) "37"
["order_id"]=>
string(3) "776"
}
}
[787]=>
array(2) {
[0]=>
array(40) {
["ID"]=>
string(1) "2"
["vendor_id"]=>
string(2) "37"
["order_id"]=>
string(3) "787"
}
}
}
Get the current
item or use reset
and extract the entire columns indexing by order_id
:获取current
项目或使用reset
并通过order_id
提取整个列索引:
$result = array_column(current($array), null, 'order_id');
If there could be multiple arrays, then just loop and append:如果可能有多个数组,则只需循环并附加:
$result = [];
foreach($array as $v) {
$result += array_column($v, null, 'order_id');
}
you can use that function你可以使用那个功能
//$array is your input array
//$mergedArray is the result wanted
$mergedArray = array_reduce($array, function($acc, $val) {
foreach ($val as $order) {
$acc[$order['order_id']] = $order;
}
return $acc;
}, []);
you can try it on http://sandbox.onlinephpfunctions.com/你可以在http://sandbox.onlinephpfunctions.com/上试试
<?php
$array = [
787 => [
0 => [ "ID" => 1, "vendor_id" => "37", "order_id" => 776],
1 => [ "ID" => 2, "vendor_id" => "37", "order_id" => 787],
2 => [ "ID" => 1, "vendor_id" => "37", "order_id" => 790],
],
734 => [
0 => [ "ID" => 1, "vendor_id" => "37", "order_id" => 722],
1 => [ "ID" => 2, "vendor_id" => "37", "order_id" => 735],
2 => [ "ID" => 1, "vendor_id" => "37", "order_id" => 734],
],
];
$t = array_reduce($array, function($acc, $val) {
foreach ($val as $order) {
$acc[$order['order_id']] = $order;
}
return $acc;
}, []);
var_dump($t);
The other answers do not maintain the multi-level structure described as your desired output.其他答案不保持描述为所需输出的多级结构。 The order_id
values must be used as first level keys, and the second level keys must be incremented/indexed as they are encountered. order_id
值必须用作第一级键,第二级键必须在遇到时递增/索引。 This will allow for multiple entries which share the same first level key.这将允许多个条目共享相同的第一级密钥。
foreach()
inside of a foreach()
: ( Demo ) foreach()
内的foreach()
:( Demo )
$result = [];
foreach ($array as $group) {
foreach ($group as $row) {
$result[$row['order_id']][] = $row;
}
}
var_export($result);
array_reduce()
with foreach()
: ( Demo ) array_reduce()
和foreach()
:(演示)
var_export(
array_reduce(
$array,
function ($carry, $group) {
foreach ($group as $row) {
$carry[$row['order_id']][] = $row;
}
return $carry;
}
)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.