【问题标题】:Get min & max value from array every `id` depend on `date` PHP每个`is`从数组中获取最小值和最大值取决于`date` PHP
【发布时间】:2018-04-28 03:52:13
【问题描述】:

我需要逻辑帮助,以便根据日期从数组中获取每个 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

我要展示的示例列表:

  • ID '35' on '2016-08-05' 最小时间是 '10:09:39';
  • ID '35' on '2016-08-05' BIGGEST TIME 是 '18:03:18';
  • ID '39' on '2016-08-05' 最小时间是 '10:09:23';
  • ID '39' on '2016-08-05' BIGGEST TIME 是 '18:03:37';

谢谢

结论: 导致数组的列表数据来自另一台机器(格式日期和时间不能更改为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";
    }
}

【问题讨论】:

    标签: php codeigniter max min


    【解决方案1】:

    您需要先对数组进行分组,按ID 分组,然后按Date 分组。将它们作为多维数组分配到一个新容器中,第一级为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

    【讨论】:

    • 正在进行尝试,等待
    • 这项工作,我结合了一个位函数来转换时间。谢谢@Ghost
    • 对不起,我还不能提供声誉,但非常感谢您的帮助@Ghost
    • @AradeaTechno 肯定有问题,很高兴这有帮助
    猜你喜欢
    • 1970-01-01
    • 2018-05-14
    • 2011-08-16
    • 1970-01-01
    • 2010-09-16
    • 2016-09-02
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多