从数组中获取最小值和最大值,每个 `is` 取决于 `date` PHP

Get min & max value from array every `id` depend on `date` PHP

我需要逻辑方面的帮助,以便根据日期为每个 ID 从数组中获取最小值和最大值。

数组示例如下:

$list_data  = array(
            array('20', '2016-08-04', '10:07:42'),
            array('20', '2016-08-04', '18:07:34'),
            array('35', '2016-08-05', '10:09:39'),
            array('35', '2016-08-05', '10:09:45'),
            array('35', '2016-08-05', '18:03:18'),
            array('35', '2016-08-05', '18:03:18'),
            array('39', '2016-08-05', '10:09:23'),
            array('39', '2016-08-05', '10:09:25'),
            array('39', '2016-08-05', '18:03:36'),
            array('39', '2016-08-05', '18:03:37'),
            array('35', '2016-08-08', '10:09:39'),
            array('35', '2016-08-08', '10:09:45'),
            array('35', '2016-08-08', '18:03:18'),
            array('35', '2016-08-08', '18:03:18'),
            array('39', '2016-08-08', '10:09:23'),
            array('39', '2016-08-08', '10:09:25'),
            array('39', '2016-08-08', '18:03:36'),
            array('39', '2016-08-08', '18:03:37'),
        );


index[0] = ID,
index[1] = Date,
index[2] = Time,

我如何打印数组

foreach($list_data as $data){
            echo $data[0];
            echo '<br>';
            echo $data[1];
            echo '<br>';
            echo $data[2];
            echo '<br>';
            echo '<br>';
        }

现在,我想要的是在每个 Date

上为每个 ID 找到 smallest timebiggest time

我要显示的示例列表:

谢谢

结论: 导致数组的列表数据来自另一台机器(格式日期和时间不能更改为unixtimestamp),所以我避免使用'strtotime'。

结果:

转换日期的函数所以我没有使用'strtotime'函数

function date_convert_to_time($date){
    $new_date   = new DateTime($date);
    return $new_date->format('U');
}

第一组数组

$arr = array();
// group them
foreach ($list_data as $v) {
    // first by id, then by date
    $arr[$v[0]][$v[1]][] = date_convert_to_time($v[1].' '.$v[2]);
}

// present it
foreach ($arr as $id => $sorted) {
    foreach ($sorted as $date => $times) {
        // $times = array_map('strtotime', $times); <--commented
        $smallest = date('H:i:s', min($times));
        $biggest = date('H:i:s', max($times));
        echo "ID '{$id}' on '{$date}' SMALLEST TIME is {$smallest}\n";
        echo "ID '{$id}' on '{$date}' BIGGEST TIME is {$biggest}\n";
    }
}

您需要先对数组进行分组,然后按 IDDate 对它们进行分组。将它们作为多维数组分配到新容器中,第一层 ID,每个 ID 每个 Date.

想法:

$arr = array();
// group them
foreach ($list_data as $v) {
    // first by id, then by date
    $arr[$v[0]][$v[1]][] = $v[2];
}

// present it
foreach ($arr as $id => $sorted) {
    foreach ($sorted as $date => $times) {
        $times = array_map('strtotime', $times);
        $smallest = date('H:i:s', min($times));
        $biggest = date('H:i:s', max($times));
        echo "ID '{$id}' on '{$date}' SMALLEST TIME is {$smallest}<br/>";
        echo "ID '{$id}' on '{$date}' BIGGEST TIME is {$biggest}<br/>";
    }
}

基本上每个时间都指定了每个日期,只是将它们简单地转换为unix时间戳并作为数组,只需使用minmax得到最低和最高

Sample Out