php - 如何访问 json 中的特定元素

标签 php telegram-bot

我有一个来自电报的 JSON,在 php 中如何找到如果它是文本则执行某些操作,但如果它是照片、音频或...则执行其他操作。这部分位于日期之后,我如何访问它?

我尝试通过索引访问它,如 ['message'][1] 但出现错误。

{
  "ok": true,
  "result": [
    {
      "update_id": 30213598,
      "message": {
        "message_id": 41,
        "from": {
          "id": 102768333,
          "first_name": "QWr_1",
          "username": "john"
        },
        "chat": {
          "id": 108768733,
          "first_name": "QWr_1",
          "username": "john"
        },
        "date": 1439233778,
        "text": "this is a test"
      }
    },
        {
      "update_id": 30213599,
      "message": {
        "message_id": 46,
        "from": {
          "id": 108768733,
          "first_name": "QWr_1",
          "username": "john"
        },
        "chat": {
          "id": 108768733,
          "first_name": "QWr_1",
          "username": "john"
        },
        "date": 1439234126,
        "photo": [
          {
            "file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABCohC6S9uPdhViIAAgI",
            "file_size": 1923,
            "width": 90,
            "height": 90
          },
          {
            "file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABOT4Q5CJoFOVVSIAAgI",
            "file_size": 24105,
            "width": 320,
            "height": 320
          },
          {
            "file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABK2WgDnJ74jlVCIAAgI",
            "file_size": 57650,
            "width": 640,
            "height": 640
          }
        ]
      }
    },

最佳答案

您需要使用json_decode函数。

在下面的示例中,我使用 foreach 循环,然后 echo $item['message']['message_id'] 来显示 message_id

尝试示例

$json = <<<JSON
{
  "ok": true,
  "result": [
    {
      "update_id": 30213598,
      "message": {
        "message_id": 41,
        "from": {
          "id": 102768333,
          "first_name": "QWr_1",
          "username": "john"
        },
        "chat": {
          "id": 108768733,
          "first_name": "QWr_1",
          "username": "john"
        },
        "date": 1439233778,
        "text": "this is a test"
      }
    },
        {
      "update_id": 30213599,
      "message": {
        "message_id": 46,
        "from": {
          "id": 108768733,
          "first_name": "QWr_1",
          "username": "john"
        },
        "chat": {
          "id": 108768733,
          "first_name": "QWr_1",
          "username": "john"
        },
        "date": 1439234126,
        "photo": [
          {
            "file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABCohC6S9uPdhViIAAgI",
            "file_size": 1923,
            "width": 90,
            "height": 90
          },
          {
            "file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABOT4Q5CJoFOVVSIAAgI",
            "file_size": 24105,
            "width": 320,
            "height": 320
          },
          {
            "file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABK2WgDnJ74jlVCIAAgI",
            "file_size": 57650,
            "width": 640,
            "height": 640
          }
        ]
      }
    }
]
}
JSON;

$json_data = json_decode($json, true);

foreach($json_data['result'] as $item)
{
    if(isset($item['message']['text']))
    {
        echo $item['message']['text']."<br />";
    }

    if(isset($item['message']['photo']))
    {
        foreach($item['message']['photo'] as $photo)
        {
            echo "file_id: ".$photo['file_id']."<br />";
            echo "file_size: ".$photo['file_size']."<br />";
        }
    }
}

关于php - 如何访问 json 中的特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31934676/

相关文章:

php - 为什么我会收到 MySQL 错误 1062?

telegram-bot - 如何通过 Telegram Bot 发送文件

GoLang Telegram-bot-api 如何询问位置?

telegram - 如何在 Telegram 机器人上创建三个破折号菜单?

php - 边栏 Google Maps API v3 使用 PHP/MYSQL 提取数据

php - XDebug 忽略 Controller 内的断点

php - 为什么成员变量通常是私有(private)的?

php - 在 YAML 文件中测试配置的位置

javascript - Telegram 框架。如何使用 telegram.forwardMessage(); 将消息转发到给定的群组、聊天或 channel ?

bash - 在 Telegram 按钮 bash 中发送自定义 URL