Hytool
Hytool

Reputation: 1368

JSON decode - Object

Below is the JSON I decoded with:

  $reqarr = json_decode($message, true);

Here is the decoded JSON:

{
"update_id" :720650368,
"message"   :
        {
         "message_id"   :14,
         "from"         :{  
                    "id":876963516,
                    "is_bot":false,
                    "first_name":"MIS",
                    "last_name":"SKH",
                    "username":"SKHMIS",
                    "language_code":"en"},
        "chat"          :{
                    "id":876963516,
                    "first_name":"MIS",
                    "last_name":"SKH",
                    "username":"SKHMIS",
                    "type":"private"},
        "date"      :1557738572,
        "text"      :"/attendance",
        "entities"  :[{"offset":0,"length":11,"type":"bot_command"}]
        }
}

There is a key called entities with object type array, which is different when compared to the other keys.

I could not get the value this this:

    $typ = $reqarr['message']['entities']['type']

I also tried with a for-each loop, as well, but it didn't work.

How to get the value of type in entities?

Upvotes: 0

Views: 233

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

You need to do:

$typ = $reqarr['message']['entities'][0]['type'];

Output:-https://3v4l.org/KQc2s

Upvotes: 2

Mehul
Mehul

Reputation: 21

Try this:

if(!isset($reqarr['message']['entities'][0])){
    $reqarr['message']['entities']=array($reqarr['message']['entities']);
}

foreach($reqarr['message']['entities'] as $entity){
   var_dump($entities);
   die();
}

Upvotes: 0

Related Questions