Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过按键拆分时间来创建新数组_Php_Arrays - Fatal编程技术网

Php 通过按键拆分时间来创建新数组

Php 通过按键拆分时间来创建新数组,php,arrays,Php,Arrays,有一个数组: $array = [ '2020-02-07' => ['09:00' => 1500, '10:30' => 1500, '11:00' => 1500], '2020-02-08' => ['09:00' => 1500, '09:30' => 1500, '10:00' => 1500] ]; 必须在以下条件下创建新阵列: 如果时间差(在数组键中)为30分钟,则写入范围;如果时间差超过30分钟,则写入相同的值。

有一个数组:

$array = [
    '2020-02-07' => ['09:00' => 1500, '10:30' => 1500, '11:00' => 1500],
    '2020-02-08' => ['09:00' => 1500, '09:30' => 1500, '10:00' => 1500]
];
必须在以下条件下创建新阵列:

如果时间差(在数组键中)为30分钟,则写入范围;如果时间差超过30分钟,则写入相同的值。结果如下:

$out = [
    '2020-02-07' => ['09:00', '10:30 - 11:00'],
    '2020-02-08' => ['09:00 - 09:30', '09:30 - 10:00']
];
我的代码:

$array = [
    '2020-02-07' => ['09:00' => 1500, '10:30' => 1500, '11:00' => 1500],
    '2020-02-08' => ['09:00' => 1500, '09:30' => 1500, '10:00' => 1500]
];

foreach ($array as $k => $a)
{
    $a = array_keys($a);
    while ($cur = current($a)) {
        $next = next($a);
        $result[$k][] = $cur . ((strtotime($next) - strtotime($cur)) === 1800 ? ' - '. $next : '');
    }
}

print_r($result);
问题是我不知道如何删除额外的(以红色突出显示)值,每个当前值必须与前一个值进行检查。告诉我你怎么做


也许有更容易解决问题的方法?谢谢。

这是解决问题的一种方法。在迭代每个日期值中的时间(但仅限于倒数第二个条目)时,我们会保留一个标志,表明下一次是否已包含在间隔中。当我们找到一个尚未包含在区间中且不属于区间的值时,我们将输出该值,否则我们将输出一个区间:

$array = [
    '2020-02-07' => ['09:00' => 1500, '10:30' => 1500, '11:00' => 1500],
    '2020-02-08' => ['09:00' => 1500, '09:30' => 1500, '10:00' => 1500],
    '2020-02-09' => ['09:00' => 1500, '09:30' => 1500, '11:00' => 1500],
    '2020-02-10' => ['09:00' => 1500, '10:30' => 1500, '11:30' => 1500]
];
$result = array();
foreach ($array as $date => $arr) {
    $next_included = false;
    $times = array_keys($arr);
    $count = count($times);
    for ($i = 0; $i < $count - 1; $i++) {
        $current = $times[$i];
        $next = $times[$i+1];
        if (strtotime($next) - strtotime($current) == 1800) {
            $result[$date][] = "$current - $next";
            $next_included = true;
        }
        else {
            if (!$next_included) $result[$date][] = $current;
            $next_included = false;
        }
    }
    if (!$next_included) {
        $result[$date][] = $next;
    }
}
print_r($result);

您的描述与$out的内容不匹配。9:00和10:30之间的差值超过30分钟,因此应打印间隔。在$array的第二行中,所有键之间的差异为30分钟,因此不应打印间隔。或者,我搞错了?为什么
['09:00'=>1500,'09:30'=>1500,'10:00'=>1500]
['09:00-09:30,'10:00']
的输出不是
['09:00-10:00']
['09:00-09:30,'09:30-10:00']
?@Nick,
['09:00-09:00-09:30,'10:00']
,对不起,我搞错了。。
Array
(
    [2020-02-07] => Array
        (
            [0] => 09:00
            [1] => 10:30 - 11:00
        )
    [2020-02-08] => Array
        (
            [0] => 09:00 - 09:30
            [1] => 09:30 - 10:00
        )
    [2020-02-09] => Array
        (
            [0] => 09:00 - 09:30
            [1] => 11:00
        )
    [2020-02-10] => Array
        (
            [0] => 09:00
            [1] => 10:30
            [2] => 11:30
        )
)