从php数组中的特定日期取出数据

时间:2017-11-01 06:38:53

标签: php arrays

我想从以下数组中提取数据,其中键3 = 2017-10-27:

Array
(
    [0] => Array
        (
            [0] => 65604
            [1] => 1
            [2] => 0
            [3] => 2017-09-04 18:22:34
        )

    [1] => Array
        (
            [0] => 69
            [1] => 29
            [2] => 0
            [3] => 2017-10-27 07:27:59
        )

    [2] => Array
        (
            [0] => 70
            [1] => 1
            [2] => 0
            [3] => 2017-09-10-27:44:13
        )

    [3] => Array
        (
            [0] => 71
            [1] => 5
            [2] => 0
            [3] => 2017-09-05 07:52:54
        )

    [4] => Array
        (
            [0] => 72
            [1] => 28
            [2] => 0
            [3] => 2017-09-05 07:54:38
        )

    [5] => Array
        (
            [0] => 73
            [1] => 18
            [2] => 0
            [3] => 2017-09-05 07:54:53
        )

由于数组太大,并且包含大约20000个数据。我想提取出我需要的数据。我使用了foreach循环但是我花了太多时间,因为我也想在数据库中更新

foreach($array as $k=>$val){
        $all = new DateTime($val[3]);
        $hun = $all->format('H:i:s');
        $date= $all->format('Y-m-d');
        $id= $val[1];
        if($date=='2017-10-27'){
             //$dbh->query("UPDATE table SET date='$date' WHERE id='$id' AND date IS NULL");
        }
}

数据库没有更新,print_r()花了大约5分钟。任何更好的方法来过滤掉我需要的数据。

2 个答案:

答案 0 :(得分:1)

使用此功能重新索引数组:

function reindex($arr, $commonIndex){
    $res = array(); 
    foreach( $arr as $one=>$two)
        $res[$two[$commonIndex]] = $two;
    return $res;
}

用法:

$res = reindex($arr, 3);
echo $res['2017-10-27'];

答案 1 :(得分:0)

作为对你评论的回答:

结构示例:

array(
    "2017-10-10" => array(  /* this date is just for filtering and structure purpose */
                    69,
                    29,
                    0,
                    "2017-10-27 07:27:59" /* optional if you still want to store the date + time in the element */
                ),
    "2017-10-10" ... etc.
)