将元素推入缺少月份的数组
Push elements into array where a month is missing
我想用零填充数组中缺失的月份。
输入数据:
$d = [
[
'type' => 25500,
'month' => 'July'
],
[
'type' => 5465,
'month' => 'January'
],
[
'type' => 40000,
'month' => 'April'
],
[
'type' => 35000,
'month' => 'June'
],
[
'type' => 10000,
'month' => 'February'
]
];
$allmonths = ['January','February','March', 'April','May','June','July','August','September','October','November','December'];
我的代码:
$res = [];
foreach ($allmonths as $key => $mes) {
$teste = array_search($mes, array_column($d, 'month'));
if ($teste) {
$res[$teste] = ['type' => 2, 'moth '=> $mes];
} else {
$res[] = ['type' => 0,'moth '=> $mes];
}
}
我一直在尝试解决这个问题,但没有成功。
预期结果:
Array
(
[0] => Array
(
[type] => 25500
[month] => July
)
[1] => Array
(
[type] => 5465
[month] => January
)
[2] => Array
(
[type] => 40000
[month] => April
)
[3] => Array
(
[type] => 35000
[month] => June
)
[4] => Array
(
[type] => 10000
[month] => February
)
[5] => Array
(
[type] => 0
[month] => March
)
[6] => Array
(
[type] => 0
[month] => May
)
[7] => Array
(
[type] => 0
[month] => August
)
[8] => Array
(
[type] => 0
[month] => September
)
[9] => Array
(
[type] => 0
[month] => October
)
[10] => Array
(
[type] => 0
[month] => November
)
[11] => Array
(
[type] => 0
[month] => December
)
)
类似这样的方法可行:
<?php
$d = [
[
'type' => 25500,
'month' => 'July'
],
[
'type' => 5465,
'month' => 'January'
],
[
'type' => 40000,
'month' => 'April'
],
[
'type' => 35000,
'month' => 'June'
],
[
'type' => 10000,
'month' => 'February'
]
];
$allmonths = ['January','February','March', 'April','May','June','July','August','September','October','November','December'];
$present_months = array_column($d, 'month');
foreach ($allmonths as $month) {
if (!in_array($month, $present_months)) {
$d[] = [
'type' => 0,
'month' => $month,
];
}
}
var_dump($d);
由于这里的objective是将缺失的月份追加到原始数组$d
而不改变现有的elements
(按照预期结果),我们可以简单地改变$d
而不是创建新变量 $res
.
同时转换数组的键 $d
将使它更容易使用。这确实假设 month
在 $d
.
中只出现一次
## Assumption: Month is unique
$dNew = array_column($d, null, 'month');
$res = [];
foreach ($allmonths as $key => $mes) {
if (!isset($dNew[$mes])) {
$d[] = ['type' => 0, 'moth ' => $mes];
}
}
var_dump($d);
如果 month
在数组中不是唯一的,您可以使用 array_search
或 in_array
来获得类似的结果。
$dNew = array_column($d, 'month');
$res = [];
foreach ($allmonths as $key => $mes) {
if (!array_search($mes, $dNew)) {
$d[] = ['type' => 0, 'moth ' => $mes];
}
}
print_r($d);
我会选择 array_column(), array_diff() 和一个 foreach()
进程:
1)创建所有月份的数组
2)使用 array_column()
.
获取数组中的所有月份
3)使用 array_diff()
.
查找数组中不可用的月份
4)迭代这个差异并将它们添加到当前数组。
$allMonths = ['January','February','March', 'April','May','June','July','August','September','October','November','December'];
$currentMonths = array_column($d, 'month');
$notIncludedMonth = array_diff($allMonths,$currentMonths);
foreach ($notIncludedMonth as $month) {
$d[] = [
'type' => 0,
'month' => $month,
];
}
print_r($d);
这个答案需要付出额外的努力来安排月份(不是你要求的——我只是觉得合适)。
此外,效率可能不是这项任务的重点,但为了研究人员的利益,我将表达 in_array()
、array_search()
、array_diff()
和任何其他“值比较”技术无法像 array_key_exists()
、isset()
、??
(空合并运算符)、array_diff_key()
等“键比较”技术那样高效地工作。出于这个原因,我赞成努力的第一个片段——!isset()
将在一个循环中表现得很好。
这是我的替代方案,它不进行迭代函数调用(但会迭代整个月份数组)。 (Demo)
$lookup = array_column($d, null, 'month'); // assign temporary keys for improved efficiency
$result = [];
foreach ($allmonths as $month) {
$result[] = $lookup[$month] ?? ['type' => 0, 'month' => $month];
}
var_export($result);
我想用零填充数组中缺失的月份。
输入数据:
$d = [
[
'type' => 25500,
'month' => 'July'
],
[
'type' => 5465,
'month' => 'January'
],
[
'type' => 40000,
'month' => 'April'
],
[
'type' => 35000,
'month' => 'June'
],
[
'type' => 10000,
'month' => 'February'
]
];
$allmonths = ['January','February','March', 'April','May','June','July','August','September','October','November','December'];
我的代码:
$res = [];
foreach ($allmonths as $key => $mes) {
$teste = array_search($mes, array_column($d, 'month'));
if ($teste) {
$res[$teste] = ['type' => 2, 'moth '=> $mes];
} else {
$res[] = ['type' => 0,'moth '=> $mes];
}
}
我一直在尝试解决这个问题,但没有成功。
预期结果:
Array
(
[0] => Array
(
[type] => 25500
[month] => July
)
[1] => Array
(
[type] => 5465
[month] => January
)
[2] => Array
(
[type] => 40000
[month] => April
)
[3] => Array
(
[type] => 35000
[month] => June
)
[4] => Array
(
[type] => 10000
[month] => February
)
[5] => Array
(
[type] => 0
[month] => March
)
[6] => Array
(
[type] => 0
[month] => May
)
[7] => Array
(
[type] => 0
[month] => August
)
[8] => Array
(
[type] => 0
[month] => September
)
[9] => Array
(
[type] => 0
[month] => October
)
[10] => Array
(
[type] => 0
[month] => November
)
[11] => Array
(
[type] => 0
[month] => December
)
)
类似这样的方法可行:
<?php
$d = [
[
'type' => 25500,
'month' => 'July'
],
[
'type' => 5465,
'month' => 'January'
],
[
'type' => 40000,
'month' => 'April'
],
[
'type' => 35000,
'month' => 'June'
],
[
'type' => 10000,
'month' => 'February'
]
];
$allmonths = ['January','February','March', 'April','May','June','July','August','September','October','November','December'];
$present_months = array_column($d, 'month');
foreach ($allmonths as $month) {
if (!in_array($month, $present_months)) {
$d[] = [
'type' => 0,
'month' => $month,
];
}
}
var_dump($d);
由于这里的objective是将缺失的月份追加到原始数组$d
而不改变现有的elements
(按照预期结果),我们可以简单地改变$d
而不是创建新变量 $res
.
同时转换数组的键 $d
将使它更容易使用。这确实假设 month
在 $d
.
## Assumption: Month is unique
$dNew = array_column($d, null, 'month');
$res = [];
foreach ($allmonths as $key => $mes) {
if (!isset($dNew[$mes])) {
$d[] = ['type' => 0, 'moth ' => $mes];
}
}
var_dump($d);
如果 month
在数组中不是唯一的,您可以使用 array_search
或 in_array
来获得类似的结果。
$dNew = array_column($d, 'month');
$res = [];
foreach ($allmonths as $key => $mes) {
if (!array_search($mes, $dNew)) {
$d[] = ['type' => 0, 'moth ' => $mes];
}
}
print_r($d);
我会选择 array_column(), array_diff() 和一个 foreach()
进程:
1)创建所有月份的数组
2)使用 array_column()
.
3)使用 array_diff()
.
4)迭代这个差异并将它们添加到当前数组。
$allMonths = ['January','February','March', 'April','May','June','July','August','September','October','November','December'];
$currentMonths = array_column($d, 'month');
$notIncludedMonth = array_diff($allMonths,$currentMonths);
foreach ($notIncludedMonth as $month) {
$d[] = [
'type' => 0,
'month' => $month,
];
}
print_r($d);
这个答案需要付出额外的努力来安排月份(不是你要求的——我只是觉得合适)。
此外,效率可能不是这项任务的重点,但为了研究人员的利益,我将表达 in_array()
、array_search()
、array_diff()
和任何其他“值比较”技术无法像 array_key_exists()
、isset()
、??
(空合并运算符)、array_diff_key()
等“键比较”技术那样高效地工作。出于这个原因,我赞成努力的第一个片段——!isset()
将在一个循环中表现得很好。
这是我的替代方案,它不进行迭代函数调用(但会迭代整个月份数组)。 (Demo)
$lookup = array_column($d, null, 'month'); // assign temporary keys for improved efficiency
$result = [];
foreach ($allmonths as $month) {
$result[] = $lookup[$month] ?? ['type' => 0, 'month' => $month];
}
var_export($result);