简体   繁体   中英

how can I access to a specific element in json

I have a JSON from telegram, In php How can find that if it is a text do something, but if its photo, audio or ... do something else. this part is located after date, how can I access it?

I tried by access it by index like ['message'][1] but I got error.

{
  "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
          }
        ]
      }
    },

You need to use json_decode function.

I use foreach loop then echo $item['message']['message_id'] to display message_id in following example.

Try Example

$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 />";
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM