使用 PHP 从 JSON 数据中提取节目信息

本文介绍如何使用 PHP 从一个 JSON 数据源中提取特定频道和日期的节目信息,并将其格式化为 JSON 输出。示例代码展示了如何解析 JSON 结构,提取相关数据,并将结果输出为可读的 JSON 格式。

JSON 数据示例:

{
  "@attributes": {
    "info-name": "Sage",
    "info-url": "https://github.com/OpenAI/gpt/blob/main/samples/dialogue.py"
  },
  "channel": [
    {
      "@attributes": {
        "id": "重庆新闻高清",
        "display-name": "重庆新闻高清"
      },
      "display-name": "重庆新闻高清"
    },
    {
      "@attributes": {
        "id": "重庆卫视高清",
        "display-name": "重庆卫视高清"
      },
      "display-name": "重庆卫视高清"
    }
  ],
  "programme": [
    {
      "@attributes": {
        "channel": "重庆新闻高清",
        "start": "20230908001000 +0800",
        "stop": "20230908005400 +0800"
      },
      "title": "网罗天下"
    },
    {
      "@attributes": {
        "channel": "重庆新闻高清",
        "start": "20230908005400 +0800",
        "stop": "20230908010800 +0800"
      },
      "title": "重庆专访"
    },
    {
      "@attributes": {
        "channel": "重庆卫视高清",
        "start": "20230908000500 +0800",
        "stop": "20230908003700 +0800"
      },
      "title": "纪录片"
    },
    {
      "@attributes": {
        "channel": "重庆卫视高清",
        "start": "20230908003700 +0800",
        "stop": "20230908012000 +0800"
      },
      "title": "剧场"
    }
  ]
}

PHP 代码:

<?php

$json = '{
  "@attributes": {
    "info-name": "Sage",
    "info-url": "https://github.com/OpenAI/gpt/blob/main/samples/dialogue.py"
  },
  "channel": [
    {
      "@attributes": {
        "id": "重庆新闻高清",
        "display-name": "重庆新闻高清"
      },
      "display-name": "重庆新闻高清"
    },
    {
      "@attributes": {
        "id": "重庆卫视高清",
        "display-name": "重庆卫视高清"
      },
      "display-name": "重庆卫视高清"
    }
  ],
  "programme": [
    {
      "@attributes": {
        "channel": "重庆新闻高清",
        "start": "20230908001000 +0800",
        "stop": "20230908005400 +0800"
      },
      "title": "网罗天下"
    },
    {
      "@attributes": {
        "channel": "重庆新闻高清",
        "start": "20230908005400 +0800",
        "stop": "20230908010800 +0800"
      },
      "title": "重庆专访"
    },
    {
      "@attributes": {
        "channel": "重庆卫视高清",
        "start": "20230908000500 +0800",
        "stop": "20230908003700 +0800"
      },
      "title": "纪录片"
    },
    {
      "@attributes": {
        "channel": "重庆卫视高清",
        "start": "20230908003700 +0800",
        "stop": "20230908012000 +0800"
      },
      "title": "剧场"
    }
  ]
}';

$data = json_decode($json, true);

$channelName = "重庆新闻";
$date = "20230908";

$result = array();
$result["channel_name"] = $channelName . "高清";
$result["date"] = substr($date, 0, 4) . "-" . substr($date, 4, 2) . "-" . substr($date, 6, 2);
$result["url"] = "http://baidu.com";

$epgData = array();

foreach ($data["programme"] as $programme) {
    if ($programme["@attributes"]["channel"] == $channelName . "高清") {
        $epgItem = array();
        $epgItem["start"] = substr($programme["@attributes"]["start"], 8, 2) . ":" . substr($programme["@attributes"]["start"], 10, 2);
        $epgItem["end"] = substr($programme["@attributes"]["stop"], 8, 2) . ":" . substr($programme["@attributes"]["stop"], 10, 2);
        $epgItem["title"] = $programme["title"];
        $epgData[] = $epgItem;
    }
}

$result["epg_data"] = $epgData;

$output = json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo $output;
?>

输出结果:

{
    "channel_name": "重庆新闻高清",
    "date": "2023-09-08",
    "url": "http://baidu.com",
    "epg_data": [
        {
            "start": "00:10",
            "end": "00:54",
            "title": "网罗天下"
        },
        {
            "start": "00:54",
            "end": "01:08",
            "title": "重庆专访"
        }
    ]
}

代码说明:

  1. 使用 json_decode 函数将 JSON 字符串解析为 PHP 数组。
  2. 获取频道名称和日期信息,并进行格式化。
  3. 使用 foreach 循环遍历 programme 数组,查找指定频道和日期的节目信息。
  4. 从节目信息中提取开始时间、结束时间和节目标题,并将结果保存到 epgData 数组中。
  5. 使用 json_encode 函数将最终结果格式化为 JSON 字符串并输出。

本例展示了如何使用 PHP 从 JSON 数据中提取特定频道和日期的节目信息,您可以根据实际情况修改代码以提取其他信息或进行更复杂的操作。

使用 PHP 从 JSON 数据中提取节目信息

原文地址: https://www.cveoy.top/t/topic/offU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录