白天获取数组的特定部分

时间:2014-11-01 22:07:02

标签: php mysql arrays sorting

我有一个包含Youtube视频的阵列,白天我想要一个特定的视频。

Array
(

[10] => Array
    (
        [video_id] => 2
        [show_date] => 2014-11-01
        [show_time] => 14:40:00
        [video_duration] => 5470
        [video_youtube_id] => MTLObnglz_U
        [video_title] => Out Of Bounds (OV)
        [source] => channel: netzkino
    )

[11] => Array
    (
        [video_id] => 155
        [show_date] => 2014-11-01
        [show_time] => 21:10:00
        [video_duration] => 9392
        [video_youtube_id] => OO-e8_-bMv8
        [video_title] => Foo Fighters Live at Lollapalooza Brazil 2012 Full Concert HD 720p
        [source] => 
    )

[14] => Array
    (
        [video_id] => 3
        [show_date] => 2014-11-01
        [show_time] => 23:59:00
        [video_duration] => 5520
        [video_youtube_id] => cf7Eu9WJ_ek
        [video_title] => City of Sex (Komödie mit Nicole Kidman)
        [source] => 
    )

所以当它介于“2014-11-01 21:10:00”和(+ video_duration)“2014-11-01 23:46:31”之后我想得到“Foo Fighters”:

[11] => Array
    (
        [video_id] => 155
        [show_date] => 2014-11-01
        [show_time] => 21:10:00
        [video_duration] => 9392
        [video_youtube_id] => OO-e8_-bMv8
        [video_title] => Foo Fighters Live at Lollapalooza Brazil 2012 Full Concert HD 720p
        [source] => 
    )

1 个答案:

答案 0 :(得分:0)

最简单的方法是在数组上使用foreach()并检查每条记录。像这样:

$array = array(...); // Your array
$newArray = array();
$currentTime = time();

foreach ($array as $key => $value) {

    $fromTime = strtotime($value['show_date'] . ' ' . $value['show_time']);
    $toTime = $fromTime + $value['video_duration'];

    if ($fromTime <= $currentTime && $toTime >= $currentTime) {
        $newArray[] = $value;
    }
}