简体   繁体   中英

Access element of JSON Object which is in an Array

How can you access a JSON object element that is in an array?

Ideally I would like to know how to do this if the array is of unknown size and also an unknown amount of JSON objects.

In the example below I would like to access id in JSON object 0 and 19.

array:1 [▼
  0 => {#411 ▼
    +0: {#157 ▶}
    +1: {#167 ▶}
    +2: {#192 ▶}
    +3: {#200 ▶}
    +4: {#206 ▶}
    +5: {#227 ▶}
    +6: {#235 ▶}
    +7: {#259 ▶}
    +8: {#269 ▶}
    +9: {#281 ▶}
    +10: {#299 ▶}
    +11: {#308 ▶}
    +12: {#316 ▶}
    +13: {#325 ▶}
    +14: {#335 ▶}
    +15: {#352 ▶}
    +16: {#362 ▶}
    +17: {#380 ▶}
    +18: {#390 ▶}
    +19: {#402 ▼
      +"created_at": "Mon Jan 23"
      +"id": 823548040000000000
      +"id_str": "823548040000000000"
      +"text": "blah blah blah blah blah blah blah"
      +"truncated": true
      +"entities": {#403 ▶}
      +"source": "<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>"
      +"in_reply_to_status_id": null
      +"in_reply_to_status_id_str": null
      +"in_reply_to_user_id": null
      +"in_reply_to_user_id_str": null
      +"in_reply_to_screen_name": null
      +"user": {#406 ▶}
      +"geo": null
      +"coordinates": null
      +"place": null
      +"contributors": null
      +"is_quote_status": false
      +"retweet_count": 3
      +"favorite_count": 8
      +"favorited": false
      +"retweeted": false
      +"possibly_sensitive": false
      +"lang": "en"
    }
  }
]

The top level is an array with one element indexed 0(first lines in your paste). Ie $var[0].

$var[0] seems to contain an object if I interpret your paste correctly(the curly brace on "0 => {"). Therefor, if you want to access its parts you use ->, in your case $var[0]->0 or $var[0]->19.

Elements 0 and 19 are objects(curly braces). So to access them you do f.ex. $var[0]->0->created_at.

Edit: Accessing numerical object properties isn't as easy as one would wish. But if you cast the object as an array it can be done:

((array) $var[0])[0]->created_at

Explanation: $var[0] is an object, but its properties are numerical. This is where the T_LNUMBER error occurs. So we cast that object as an array: (array) $var[0]. To access element with index 0 in the resulting array it is wrapped in parentheses: ((array) $var[0])[0]. (Without the parentheses("(array) $var[0][0]") it would've been a 2-dimensional array). Now we are at the object with string keys, which can be accessed as usual.

SO question goes through this in more detail.

Do note that the code won't be reusable, it's tailor made for this particular case. So if this situation occurs iin other places you should probably write some function that converts your data from objects to arrays.

If you use json_decode() it will convert the JSON into a PHP variable. Then you can access the elements however you normally would (array indices, etc).

https://secure.php.net/manual/en/function.json-decode.php

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