Php 如何循环2个数组并查找匹配/缺少的值
我目前有2个阵列Php 如何循环2个数组并查找匹配/缺少的值,php,Php,我目前有2个阵列 $last_12_months-包含从当前月份开始的最近12个月的数组 $app12\u array\u temp-包含一个查询结果,其中包含当月和每月的预约次数 我需要能够循环每个月,并为该月分配约会计数 在嵌套循环时,我得到以下结果: [0,0,0,0,0,0,0,"1",0,0,0,0,0,0,"4",0,0,0,0,0,0,"2",0,0,0,0,0,0,"15",0,0,0,0,0,0,"9",0,0,0,0,0,0,"8",0,0,0,0,0,0,"2",0,0,0
$last_12_months
-包含从当前月份开始的最近12个月的数组
$app12\u array\u temp
-包含一个查询结果,其中包含当月和每月的预约次数
我需要能够循环每个月,并为该月分配约会计数
在嵌套循环时,我得到以下结果:
[0,0,0,0,0,0,0,"1",0,0,0,0,0,0,"4",0,0,0,0,0,0,"2",0,0,0,0,0,0,"15",0,0,0,0,0,0,"9",0,0,0,0,0,0,"8",0,0,0,0,0,0,"2",0,0,0,0,0,0,"1",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
我需要的是:
[0,0,0,0,1,4,2,15,9,8,2,1]
我当前的脚本是:
//Loop through months, and check if appointments happened in that month
foreach($last_12_months as $m => $month){
foreach($app_12_array_temp AS $app){
if(date("m", strtotime($app['time'])) == date("m", strtotime($month))){
$app_12_array[] = $app['count'];
break;
}else{
$app_12_array[] = 0;
}
}
}
因此,如果当月有约会,则应将约会计数添加到数组中,否则添加0。
当前,每当循环不是相应月份时,它都会添加0
我曾尝试在条件满足时使用中断,但在找到该数据之前将返回0
我也在数组()中尝试了,但只返回[0,0,0,0,1,0,0,0,0,0,0,0]
这是脚本应该考虑的,但我想不出如何让它做到这一点:
Jan -> Any data for this month? -> No, so add 0 for this month to array
Feb -> Any data for this month? -> No, so add 0 for this month to array
Mar -> Any data for this month? -> Yes, so add $app['count'] to array
Apr -> Any data for this month? -> Yes, so add $app['count'] to array
May -> Any data for this month? -> Yes, so add $app['count'] to array
编辑
尝试此操作,使用月份作为新数组的键
$app_12_array_temp = [
['time' => '2019-07-27 13:00:00', 'count' => '1'],
['time' => '2019-08-26 13:00:00', 'count' => '2'],
['time' => '2019-09-06 13:00:00', 'count' => '8'],
['time' => '2019-10-22 12:00:00', 'count' => '9'],
['time' => '2019-11-21 12:00:00', 'count' => '15'],
['time' => '2019-12-27 11:00:00', 'count' => '2'],
['time' => '2020-01-22 15:00:00', 'count' => '4'],
['time' => '2020-02-12 09:00:00', 'count' => '1'],
];
$last_12_months = ['Feb', 'Jan', 'Dec', 'Nov', 'Oct', 'Sep', 'Aug', 'Jul', 'Jun', 'May', 'Apr', 'Mar'];
$app_12_array = array_fill_keys( $last_12_months, 0 );
foreach ( $last_12_months as $m => $month )
{
foreach ( $app_12_array_temp AS $app )
{
if ( date( "m", strtotime( $app['time'] ) ) == date( "m", strtotime( $month ) ) )
{
$app_12_array[ $month ] = ($app_12_array[ $month ] ?? 0 ) + $app['count'];
break;
}
}
}
var_dump( $app_12_array );
这是一种解决问题的方法,使用do-while循环
。通过修改$dateFirst
变量,可以轻松地将其用于比12个月更短/更长的期间
代码:
$data = [
['time' => '2019-07-27 13:00:00', 'count' => '1'],
['time' => '2019-08-26 13:00:00', 'count' => '2'],
['time' => '2019-09-06 13:00:00', 'count' => '8'],
['time' => '2019-10-22 12:00:00', 'count' => '9'],
['time' => '2019-11-21 12:00:00', 'count' => '15'],
['time' => '2019-12-27 11:00:00', 'count' => '2'],
['time' => '2020-01-22 15:00:00', 'count' => '4'],
['time' => '2020-02-12 09:00:00', 'count' => '1'],
];
usort($data, function($a, $b) {
return strtotime($a['time']) - strtotime($b['time']);
});
if (!is_array($data) || count($data) === 0) {
die('no data');
}
$dateFirst = (date('Y', time()) - 1) . date('-m', time());
// begin 11 months ago instead of 12 [start]
if (date('n', strtotime($dateFirst)) == 12) {
$dateFirst = (date('Y', strtotime($dateFirst)) + 1) . '-01';
} else {
$newMonth = (date('n', strtotime($dateFirst)) + 1);
$dateFirst = date('Y', strtotime($dateFirst)) . '-' . ($newMonth <= 9 ? '0' . $newMonth : $newMonth);
}
// begin 11 months ago instead of 12 [end]
$dateCurrent = $dateFirst;
$tsDateNow = strtotime(date('Y-m', time()));
$result = [];
do {
if (!isset($result[$dateCurrent])) {
$result[$dateCurrent] = 0;
}
for ($i = 0; $i < count($data); $i++) {
$dateCheck = date('Y-m', strtotime($data[$i]['time']));
if ($dateCheck === $dateCurrent) {
$result[$dateCurrent] += $data[$i]['count'];
}
}
if (date('n', strtotime($dateCurrent)) == 12) {
$dateCurrent = (date('Y', strtotime($dateCurrent)) + 1) . '-01';
} else {
$newMonth = (date('n', strtotime($dateCurrent)) + 1);
$dateCurrent = date('Y', strtotime($dateCurrent)) . '-' . ($newMonth <= 9 ? '0' . $newMonth : $newMonth);
}
} while (strtotime($dateCurrent) <= $tsDateNow);
// exclude this month:
// } while (strtotime($dateCurrent) < $tsDateNow);
print_r($result);
Array
(
[2019-03] => 0
[2019-04] => 0
[2019-05] => 0
[2019-06] => 0
[2019-07] => 1
[2019-08] => 2
[2019-09] => 8
[2019-10] => 9
[2019-11] => 15
[2019-12] => 2
[2020-01] => 4
[2020-02] => 1
)
如果在同一个月内有多个日期不同的元素,则此代码还将对所有计数进行求和
因此,如果您有以下数组:
$data = [
['time' => '2019-07-27 13:00:00', 'count' => '1'],
['time' => '2019-08-26 13:00:00', 'count' => '2'],
['time' => '2019-09-06 13:00:00', 'count' => '8'],
['time' => '2019-10-22 12:00:00', 'count' => '9'],
['time' => '2019-07-04 09:00:00', 'count' => '1000'],
['time' => '2019-11-21 12:00:00', 'count' => '15'],
['time' => '2019-12-27 11:00:00', 'count' => '2'],
['time' => '2020-01-22 15:00:00', 'count' => '4'],
['time' => '2020-02-12 09:00:00', 'count' => '1'],
];
这将是输出:
Array
(
[2019-03] => 0
[2019-04] => 0
[2019-05] => 0
[2019-06] => 0
[2019-07] => 1001
[2019-08] => 2
[2019-09] => 8
[2019-10] => 9
[2019-11] => 15
[2019-12] => 2
[2020-01] => 4
[2020-02] => 1
)
您好,您可以将$last_12_months
和$app_12_arr_temp
数组的内容添加到问题中吗?完成:)将其添加为编辑“包含一个查询结果,其中包含该月和一个月内的约会次数”-不完全是,它包含一个特定的日期,而不是实际的月份。您是否已确保每个月只有一条记录,或者可以更多?每个月的结果组因此每个月只有一条记录,并统计该月有多少条记录。可以看出,在循环中,我从时间戳中提取了月份。如果分组月份没有预约,则不会返回结果。因此,我需要遍历每个月,并检查约会数组中是否有匹配的月份……或者完全去掉第二个带有月份名称的数组。您已经在第一个数组中获得了所需的所有信息,您需要做的只是将这些日期格式化为月份名称,而不是在月份中的某个位置粘贴任意日期。这将返回更接近我要查找的内容,但顺序与上一个12个月数组不同,没有数据的月份在相应月份的数组中应显示为0。您的建议的结果:{“二月”:1,“一月”:4,“十二月”:2,“十一月”:15,“十月”:9,“九月”:8,“八月”:2,“七月”:1}(请参见编辑)您可以使用0初始化“模板”数组,但您的解决方案有问题,结果发现它不喜欢将$month转换为日期()。。。所以只将其保留为$month而不是DATE('m',strottime($month))
Array
(
[2019-03] => 0
[2019-04] => 0
[2019-05] => 0
[2019-06] => 0
[2019-07] => 1001
[2019-08] => 2
[2019-09] => 8
[2019-10] => 9
[2019-11] => 15
[2019-12] => 2
[2020-01] => 4
[2020-02] => 1
)