当值相同时,合并 2 个数组并合并到第三个新数组中



我有一个名为作业计数的数组,如下所示$job=>

Array
(
[0] => Array([count] => 3[yearmonth] => 2019-7)

[1] => Array([count] => 3[yearmonth] => 2019-9)

[2] => Array([count] => 5[yearmonth] => 2019-10))           
)

第二个日期数组为$date=>

Array
([0] => Array([yearmonth] => 2019-6)

[1] => Array([yearmonth] => 2019-7)

[2] => Array([yearmonth] => 2019-8)

[3] => Array([yearmonth] => 2019-9)

[4] => Array([yearmonth] => 2019-10)
)

作业数组不是连续数组。所以在这里我想要的是,如果一个条目在$date中可用,但在$job中不存在,然后创建第三个数组,计数 = 0 和年月值。 如下所示

Array
([0] => Array([count] => 0[yearmonth] => 2019-6)

[1] => Array([count] => 3[yearmonth] => 2019-7)

[2] => Array([count] => 0[yearmonth] => 2019-8)

[3] => Array([count] => 3[yearmonth] => 2019-9)

[4] => Array([count] => 5[yearmonth] => 2019-10)
)

要在迭代$dates数组时有效地在$jobs数组中搜索日期,请创建一个"查找"数组。

查找应具有表示关系数据的键 (yearmonth(。

使用$dates数组中遇到的每个日期,检查yearmonth值是否表示为查找中的键 - 如果是,请使用count值,如果没有,则设置 0。

变量前面的&表示"通过引用修改 - 以便原始数组发生突变,而不需要声明新的输出数组。

??是"空合并运算符",则允许在变量null/未声明时使用回退值。

代码:(演示(

$jobs = [
['count' => 3, 'yearmonth' => '2019-7'],
['count' => 3, 'yearmonth' => '2019-9'],
['count' => 5, 'yearmonth' => '2019-10'],         
];
$dates = [
['yearmonth' => '2019-6'],
['yearmonth' => '2019-7'],
['yearmonth' => '2019-8'],
['yearmonth' => '2019-9'],
['yearmonth' => '2019-10'],
];
$lookup = array_column($jobs, 'count', 'yearmonth');
foreach ($dates as &$date) {
$date['count'] = $lookup[$date['yearmonth']] ?? 0;
}
var_export($dates);

输出:

array (
0 => 
array (
'yearmonth' => '2019-6',
'count' => 0,
),
1 => 
array (
'yearmonth' => '2019-7',
'count' => 3,
),
2 => 
array (
'yearmonth' => '2019-8',
'count' => 0,
),
3 => 
array (
'yearmonth' => '2019-9',
'count' => 3,
),
4 => 
array (
'yearmonth' => '2019-10',
'count' => 5,
),
)

或者,如果您要求声明一个新的输出数组,并且关联子数组必须与您的问题中的顺序相同,那么这是我对相同技术的调整:

代码:(演示(

$lookup = array_column($jobs, null, 'yearmonth');
$result = [];
foreach ($dates as $date) {
$result[] = $lookup[$date['yearmonth']] ?? ['count' => 0] + $date;
}
var_export($result);

查找数组现在包含完整的子数组数据。array_column()使用null作为第二个参数以防止隔离单行,并使用yearmonth作为第三个参数来分配第一级键。

foreach 循环不再"通过引用进行修改"。 null 合并运算符仍用于避免调用isset(),非常适合简洁地写入回退数据。 我在包含count元素的硬编码数组和$date数组之间使用"联合运算符"——避免调用array_merge()或手动编写['yearmonth' => $date['yearmonth']。 这是一种合适的合并技术,因为键是每个子数组的唯一且具有关联的。

永远不要低估能够在 php 数组中以非常灵活的方式/方式使用键的能力。

<?php
//Copied from mickmackusa
$jobs = [
['count' => 3, 'yearmonth' => '2019-7'],
['count' => 3, 'yearmonth' => '2019-9'],
['count' => 5, 'yearmonth' => '2019-10'],         
];
$dates = [
['yearmonth' => '2019-6'],
['yearmonth' => '2019-7'],
['yearmonth' => '2019-8'],
['yearmonth' => '2019-9'],
['yearmonth' => '2019-10'],
];
//End copy
/*
Make the keys of $jobs be the same as values
of yearmonth: (This makes it easy to compare later on)
Array
(
[0] => Array
(
[count] => 3
[yearmonth] => 2019-7
)
[1] => Array
(
[count] => 3
[yearmonth] => 2019-9
)
[2] => Array
(
[count] => 5
[yearmonth] => 2019-10
)
[2019-7] => Array
(
[count] => 3
[yearmonth] => 2019-7
)
[2019-9] => Array
(
[count] => 3
[yearmonth] => 2019-9
)
[2019-10] => Array
(
[count] => 5
[yearmonth] => 2019-10
)
)
*/
foreach($jobs as $key=>$item) {
$jobs[$item['yearmonth']] = $item;
}
//Create a third array $third_arr based on your $jobs and $dates array
$third_arr = [];
foreach($dates as $item) {
$key_value = $item['yearmonth'];
if (isset($jobs[$key_value]['yearmonth'])) {
//Available in dates and present in $jobs
//Just copy values from the $jobs item which relates to this yearmonth
$third_arr[] = $jobs[$key_value];
}
else {
//Available in dates but not present in $job
$third_arr[] = ['count'=>0, 'yearmonth'=>$key_value];
}
}

第三个阵列$third_arr的输出:

Array
(
[0] => Array
(
[count] => 0
[yearmonth] => 2019-6
)
[1] => Array
(
[count] => 3
[yearmonth] => 2019-7
)
[2] => Array
(
[count] => 0
[yearmonth] => 2019-8
)
[3] => Array
(
[count] => 3
[yearmonth] => 2019-9
)
[4] => Array
(
[count] => 5
[yearmonth] => 2019-10
)
)

上面代码的更压缩版本如下所示:

foreach($jobs as $key=>$item) {
$jobs[$item['yearmonth']] = $item;
}
$third_arr = [];
foreach($dates as $item) {
$kvalue = $item['yearmonth'];
isset($jobs[$kvalue]['yearmonth']) ? 
$third_arr[] = $jobs[$kvalue] : $third_arr[] = ['count'=>0, 'yearmonth'=>$kvalue];
}

因此,如果您将count => 0添加到$date数组并在yearmonth上索引,则可以在yearmonth上索引$job并将其合并到$date

$result = array_merge(array_column(array_map(function($v) { $v['count'] = 0; return $v; },
$date), null, 'yearmonth'),
array_column($job, null, 'yearmonth'));

相关内容

  • 没有找到相关文章

最新更新