3 回答
TA贡献1811条经验 获得超6个赞
$jobs
要在迭代数组时有效地搜索数组中的日期$dates
,请创建一个“查找”数组。
查找应该具有代表关系数据的键 ( 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 循环不再“通过引用修改”。空合并运算符仍然用于避免调用isset(),并且非常适合简洁地编写后备数据。count我在包含元素的硬编码数组和数组之间使用“联合运算符” $date——避免调用array_merge()或手动编写['yearmonth' => $date['yearmonth']. 这是一种合适的合并技术,因为键对于每个子数组来说都是唯一的且关联的。
TA贡献1936条经验 获得超6个赞
永远不要低估能够在 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];
}
TA贡献1786条经验 获得超11个赞
因此,如果您添加count => 0到$date数组和索引,yearmonth您可以索引$job并将yearmonth其合并到$date:
$result = array_merge(array_column(array_map(function($v) { $v['count'] = 0; return $v; },
$date), null, 'yearmonth'),
array_column($job, null, 'yearmonth'));
- 3 回答
- 0 关注
- 162 浏览
添加回答
举报