如何在PHP中合并两个不相等的多维数组?
How to merge two not equal multidimensional array in PHP?
我有两个数组多维数组,如 JS fiddle 中给出的那样。如果 month1 值相同,我想按升序将它合并为一个,否则按原样打印
$array1= [0] => Array
(
[month1] => January
[2020cs] => 84
[2020as] => 500
[2019cs] => 17
[2019as] => 500
)
[1] => Array
(
[month1] => February
[2020cs] => 54
[2020as] => 200
[2019cs] => 12
[2019as] => 1000
)
[2] => Array
(
[month1] => April
[2020cs] => 4
[2020as] => 100
[2019cs] => 12
[2019as] => 1400
)
[3] => Array
(
[month1] => November
[2020cs] => 0
[2020as] => 0
[2019cs] => 7
[2019as] => 200
)
)
$array2= [0] => Array
(
[month1] => January
[2020cr] => 13
[2020ar] => 300
[2019cr] => 0
[2019ar] => 0
)
[2] => Array
(
[month1] => March
[2020cr] => 1
[2020ar] => 100
[2019cr] => 0
[2019ar] => 0
)
[3] => Array
(
[month1] => November
[2020cr] => 0
[2020ar] => 0
[2019cr] => 1
[2019ar] => 800
)
[4] => Array
(
[month1] => December
[2020cr] => 0
[2020ar] => 0
[2019cr] => 2
[2019ar] => 500
)
)
我要合并如下
$array_merge= [0] => Array
(
[month1] => January
[2020cs] => 84
[2020as] => 500
[2019cs] => 17
[2019as] => 500
[2020cr] => 13
[2020ar] => 300
[2019cr] => 0
[2019ar] => 0
)
[1] => Array
(
[month1] => February
[2020cs] => 54
[2020as] => 200
[2019cs] => 12
[2019as] => 1000
)
[2] => Array
(
[month1] => March
[2020cr] => 1
[2020ar] => 100
[2019cr] => 0
[2019ar] => 0
)
[3] => Array
(
[month1] => April
[2020cs] => 4
[2020as] => 100
[2019cs] => 12
[2019as] => 1400
)
[4] => Array
(
[month1] => November
[2020cs] => 0
[2020as] => 0
[2019cs] => 7
[2019as] => 200
[2020cr] => 0
[2020ar] => 0
[2019cr] => 1
[2019ar] => 800
)
[4] => Array
(
[month1] => December
[2020cr] => 0
[2020ar] => 0
[2019cr] => 2
[2019ar] => 500
)
)
我也用过array_merge_recursive()。它也不起作用。谁能帮我吗?
提前致谢。
以下逻辑可能对您有所帮助:
- 为每个顶级数组键指定月份名称 (
rekey()
)
- 合并两个数组
- 对结果集进行排序(
$result
)
<?php
// top-level array from indexed to associative (name of month)
$array1 = rekey($array1);
$array2 = rekey($array2);
function rekey(array $arr = []): array
{
foreach ($arr as $key => $record) {
$arr[$record['month1']] = $arr[$key]; // indexed to name of month
unset($arr[$key]);
}
return $arr;
}
// run the merge
$result = array_merge_recursive($array1, $array2);
// clean up double entries key 'month1'
foreach($result as $key => &$value) {
if(is_array($value['month1'])) $value['month1'] = $value['month1'][0];
}
// prepare result set for sorting
$months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December'];
foreach($result as $key0 => $value0) {
foreach($months as $key1 => $value1) {
if($key0 === $value1) {
$result[$key1] = $result[$key0]; // set index based on month 0-11
unset($result[$key0]);
}
}
}
ksort($result); // sort
$result = array_values($result); // top-level array back to sequential index
工作demo
我有两个数组多维数组,如 JS fiddle 中给出的那样。如果 month1 值相同,我想按升序将它合并为一个,否则按原样打印
$array1= [0] => Array
(
[month1] => January
[2020cs] => 84
[2020as] => 500
[2019cs] => 17
[2019as] => 500
)
[1] => Array
(
[month1] => February
[2020cs] => 54
[2020as] => 200
[2019cs] => 12
[2019as] => 1000
)
[2] => Array
(
[month1] => April
[2020cs] => 4
[2020as] => 100
[2019cs] => 12
[2019as] => 1400
)
[3] => Array
(
[month1] => November
[2020cs] => 0
[2020as] => 0
[2019cs] => 7
[2019as] => 200
)
)
$array2= [0] => Array
(
[month1] => January
[2020cr] => 13
[2020ar] => 300
[2019cr] => 0
[2019ar] => 0
)
[2] => Array
(
[month1] => March
[2020cr] => 1
[2020ar] => 100
[2019cr] => 0
[2019ar] => 0
)
[3] => Array
(
[month1] => November
[2020cr] => 0
[2020ar] => 0
[2019cr] => 1
[2019ar] => 800
)
[4] => Array
(
[month1] => December
[2020cr] => 0
[2020ar] => 0
[2019cr] => 2
[2019ar] => 500
)
)
我要合并如下
$array_merge= [0] => Array
(
[month1] => January
[2020cs] => 84
[2020as] => 500
[2019cs] => 17
[2019as] => 500
[2020cr] => 13
[2020ar] => 300
[2019cr] => 0
[2019ar] => 0
)
[1] => Array
(
[month1] => February
[2020cs] => 54
[2020as] => 200
[2019cs] => 12
[2019as] => 1000
)
[2] => Array
(
[month1] => March
[2020cr] => 1
[2020ar] => 100
[2019cr] => 0
[2019ar] => 0
)
[3] => Array
(
[month1] => April
[2020cs] => 4
[2020as] => 100
[2019cs] => 12
[2019as] => 1400
)
[4] => Array
(
[month1] => November
[2020cs] => 0
[2020as] => 0
[2019cs] => 7
[2019as] => 200
[2020cr] => 0
[2020ar] => 0
[2019cr] => 1
[2019ar] => 800
)
[4] => Array
(
[month1] => December
[2020cr] => 0
[2020ar] => 0
[2019cr] => 2
[2019ar] => 500
)
)
我也用过array_merge_recursive()。它也不起作用。谁能帮我吗? 提前致谢。
以下逻辑可能对您有所帮助:
- 为每个顶级数组键指定月份名称 (
rekey()
) - 合并两个数组
- 对结果集进行排序(
$result
)
<?php
// top-level array from indexed to associative (name of month)
$array1 = rekey($array1);
$array2 = rekey($array2);
function rekey(array $arr = []): array
{
foreach ($arr as $key => $record) {
$arr[$record['month1']] = $arr[$key]; // indexed to name of month
unset($arr[$key]);
}
return $arr;
}
// run the merge
$result = array_merge_recursive($array1, $array2);
// clean up double entries key 'month1'
foreach($result as $key => &$value) {
if(is_array($value['month1'])) $value['month1'] = $value['month1'][0];
}
// prepare result set for sorting
$months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December'];
foreach($result as $key0 => $value0) {
foreach($months as $key1 => $value1) {
if($key0 === $value1) {
$result[$key1] = $result[$key0]; // set index based on month 0-11
unset($result[$key0]);
}
}
}
ksort($result); // sort
$result = array_values($result); // top-level array back to sequential index
工作demo