我有一个名为作业计数的数组,如下所示:$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)
)发布于 2020-06-25 05:33:49
千万不要低估在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];
}发布于 2020-06-24 22:52:52
要在迭代$jobs数组时有效地搜索$dates数组的日期,请创建一个“查找”数组。
查找应该包含表示关系数据(yearmonth)的键。
使用$dates数组中遇到的每个日期,检查yearmonth值是否表示为查找中的键--如果是,则使用count值,如果没有,则设置0。
变量之前的&意味着“通过引用修改--这样原始数组就会发生变异,而不需要声明一个新的输出数组。
??是“空合并运算符”,如果变量是null/undeclared,则允许使用回退值。
代码:(演示)
$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循环不再“通过引用修改”。空合并操作符仍然用于避免isset()的调用,并且是简洁地编写回退数据的理想方法。我正在使用包含count元素和$date数组的硬编码数组之间的“联合运算符”--避免调用array_merge()或手动编写['yearmonth' => $date['yearmonth']。这是一种合适的合并技术,因为键是唯一的,并且与每个子数组相关联。
发布于 2020-06-24 23:57:05
因此,如果将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'));https://stackoverflow.com/questions/62558221
复制相似问题