如何遍历2个数组并查找匹配/缺失值

时间:2020-02-13 11:50:50

标签: php

我目前有2个数组;

False-包含当月以来最近12个月的数组

$last_12_months-包含查询结果,其中包含当月和一个月中进行的约会计数。

我需要能够遍历每个月,并为该月分配约会计数。

当我嵌套循环时,我得到的结果是:

$app12_array_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]

因此,如果该月有约会,则应将约会计数添加到数组,否则添加0。 当前,每当没有对应月份的循环时,它将加0

我曾尝试在条件满足时使用Breaks,但会返回0,直到找到该数据为止。

我也有//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; } } } ,但只返回tried In_Array()

这是脚本应该考虑的内容,但是我想不起来如何做到这一点:

[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

2 个答案:

答案 0 :(得分:1)

尝试使用月份作为新数组的键

$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 );

答案 1 :(得分:0)

这是使用do-while loop解决问题的一种方法。通过修改$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
)

如果您在一个月内有多个具有不同日期的元素,则此代码还将汇总所有count

因此,如果您具有以下数组:

$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
)
相关问题