PHP:循环浏览json,根据两个键值比较当前时间和时间范围

时间:2016-10-09 13:56:15

标签: php json unix epg

我基本上是想根据一些json数据创建一个epg查看器。

我需要一些帮助来弄清楚如何:

  1. 循环浏览json数据。
  2. 获取每个程序的“开始”和“停止”值(unix时间戳)。
  3. 根据开始/停止为每个程序创建一个时间范围 时间戳。
  4. 将每个程序的时间范围与当前的unix时间进行比较。
  5. 显示具有适合当前时间的时间范围的程序。
  6. 这就是json数据的样子:

    [
        {
            "id": 1,
            "programs": [
                {
                    "stop": 1475982300,
                    "start": 1475976300,
                    "category": "Serier",
                    "id": 356532927,
                    "title": "Kommissær Alleyn"
                },
                {
                    "stop": 1475983500,
                    "start": 1475982300,
                    "category": "Børn",
                    "id": 355997207,
                    "title": "Udsendelsesophør"
                },
                {
                    "stop": 1475985000,
                    "start": 1475983500,
                    "id": 358595605,
                    "title": "DR-Friland: Frilandshaven"
                },
                {
                    "stop": 1475986800,
                    "start": 1475985000,
                    "category": "Dokumentar",
                    "id": 358595606,
                    "title": "Bonderøven 2010"
                },
                {
                    "stop": 1475989500,
                    "start": 1475986800,
                    "category": "Dokumentar",
                    "id": 355933560,
                    "title": "Made in Denmark"
                },
                {
                    "stop": 1475992500,
                    "start": 1475989500,
                    "category": "Dokumentar",
                    "id": 357701600,
                    "title": "De forunderlige dyr"
                },
                {
                    "stop": 1475995500,
                    "start": 1475992500,
                    "category": "Dokumentar",
                    "id": 358745047,
                    "title": "Italien: Fra Sardinien til Abruzzo"
                },
                {
                    "stop": 1476001200,
                    "start": 1475995500,
                    "category": "Serier",
                    "id": 356295670,
                    "title": "Mord på hjernen"
                },
                {
                    "stop": 1476004800,
                    "start": 1476001200,
                    "category": "Underholdning",
                    "id": 357701601,
                    "title": "Puk og Herman går i land - Endelave"
                },
                {
                    "stop": 1476008100,
                    "start": 1476004800,
                    "category": "Dokumentar",
                    "id": 355933566,
                    "title": "Guld på godset"
                },
                {
                    "stop": 1476011700,
                    "start": 1476008100,
                    "category": "Dokumentar",
                    "id": 357701602,
                    "title": "Guld på godset"
                },
                {
                    "stop": 1476017700,
                    "start": 1476011700,
                    "category": "Serier",
                    "id": 356182011,
                    "title": "Kriminalkommissær Barnaby"
                },
                {
                    "stop": 1476023700,
                    "start": 1476017700,
                    "category": "Sport",
                    "id": 358577056,
                    "title": "HåndboldSøndag: HC Motor Zaporozhye-TTH Holstebro (m), direkte"
                },
                {
                    "stop": 1476030600,
                    "start": 1476023700,
                    "category": "Sport",
                    "id": 358577057,
                    "title": "HåndboldSøndag: Bjerringbro Silkeborg-THW Kiel (m), direkte"
                },
                {
                    "stop": 1476032400,
                    "start": 1476030600,
                    "category": "Nyheder",
                    "id": 355933567,
                    "title": "TV AVISEN med Sporten og Vejret"
                },
                {
                    "stop": 1476032700,
                    "start": 1476032400,
                    "id": 363167503,
                    "title": "Guld i Købstaden - en hurtig handel"
                },
                {
                    "stop": 1476036000,
                    "start": 1476032700,
                    "category": "Dokumentar",
                    "id": 359619002,
                    "title": "Atlanterhavet: Verdens vildeste hav"
                },
                {
                    "stop": 1476039600,
                    "start": 1476036000,
                    "category": "Serier",
                    "id": 355933569,
                    "title": "Bedrag II"
                },
                {
                    "stop": 1476042000,
                    "start": 1476039600,
                    "category": "Nyheder",
                    "id": 355933570,
                    "title": "21 Søndag"
                },
                {
                    "stop": 1476042600,
                    "start": 1476042000,
                    "category": "Sport",
                    "id": 358745049,
                    "title": "Sporten"
                },
                {
                    "stop": 1476049200,
                    "start": 1476042600,
                    "category": "Film",
                    "id": 355933571,
                    "title": "Killer Elite"
                },
                {
                    "stop": 1476054600,
                    "start": 1476049200,
                    "category": "Film",
                    "id": 356069380,
                    "title": "BlackJack"
                },
                {
                    "stop": 1476060600,
                    "start": 1476054600,
                    "category": "Serier",
                    "id": 356069381,
                    "title": "Taggart: Døden checker ind"
                },
                {
                    "stop": 1476066300,
                    "start": 1476060600,
                    "category": "Film",
                    "id": 355933572,
                    "title": "Mørkets gerninger"
                },
                {
                    "stop": 1476069000,
                    "start": 1476066300,
                    "category": "Dokumentar",
                    "id": 362546476,
                    "title": "DR1 Dokumentar: Danskere omringet af IS"
                }
            ]
        }
    ]
    

    这是我到目前为止的PHP: (我知道它并不多。我只是不知道如何从这里开始)

    <?php
    $channel = '1';
    $current_unix = time();
    $json = json_decode(file_get_contents('http://tvtid-app-backend.tv2.dk/api/tvtid/v1/dayviews/'.date('Y-m-d').'?ch='.$channel), true);
    $programs = $json[0]['programs'];
    $programs_length = count($programs);
    for ($i = 0; $i < $programs_length ; $i++)
    {
        $begin = [$programs[$i]['start']];
        $end = [$programs[$i]['stop']];
        $title = [$programs[$i]['title']];
    }
    ?>
    

    这就是输出的外观: (示例时间是16:41)

    <ul>
       <li class="program">
          <div class="title_now">TV AVISEN med Sporten og Vejret</div>
          <div class="duration_now">
          <div class="now_begin">16:30</div><span class="begin">1476030600</span>
          <div class="progress_bar"><div class="progress"></div></div>
          <div class="now_end">17:00</div><span class="end">1476032400</span></div>
       </li>
       <li class="program">
          <div class="title">Guld i Købstaden - en hurtig handel</div>
          <div class="duration">17:00-17:05</div>
       </li>
       <li class="program">
          <div class="title">Atlanterhavet: Verdens vildeste hav</div>
          <div class="duration">17:05-18:00</div>
       </li>
       <li class="program">
          <div class="title">Bedrag II</div>
          <div class="duration">18:00-19:00</div>
       </li>
    
       etc...
    
    </ul>
    

    我希望这是有道理的,有人能够帮助我。

1 个答案:

答案 0 :(得分:1)

使用foreach处理整个数组要简单得多。

$channel = '1';
$current_unix = time();

// note I have removed the TRUE option from json_decode()

$json = json_decode(file_get_contents('http://tvtid-app-backend.tv2.dk/api/tvtid/v1/dayviews/'.date('Y-m-d').'?ch='.$channel));
//print_r($json);

echo '<ul>';

foreach ($json as $data) {

    echo '<li>';
    foreach ($data->programs as $prog) {

        if ( $current_unix < $prog->start ) {

            // add your own HTML instead of the simple echo
            echo $prog->title . '<br>';
            echo $prog->start . '<br>';
            echo $prog->stop . '<br>';
            echo $prog->id . '<br>';
            if ( isset($prog->category)) {
                echo $prog->category . '<br>';
            }
        }
    }
    echo '</li>';
}
echo '</ul>';