1

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"
 }
 }
]
asked Jan 27, 2017 at 17:38
2
  • echo $arr[0] and echo $arr[19]? Commented Jan 27, 2017 at 17:41
  • That will give me access to the array (my example has the array with a length of 1 but not sure how to get in to the JSON object from there. Commented Jan 27, 2017 at 17:52

2 Answers 2

3

The top level is an array with one element indexed 0(first lines in your paste). I.e. $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.

answered Jan 27, 2017 at 17:45
Sign up to request clarification or add additional context in comments.

5 Comments

You interpreted the paste correctly... sorry if I made it difficult to do so. This is what I originally thought too but I get this error: Parse error: syntax error, unexpected '19' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
@ColinStadig When do you get that error? When running json_decode()? Or somewhere in your code(if so, can you show it?)?
$favorites = $this->client->favorites_list(); $favoritearray[] = $favorites; dd($favoritearray[0]->19->id); Note that I'm using Laravel framework hence the dd. The JSON is coming from the Twitter API.
Ahh, you're right. Numerical object properties. Updating my answer.
That did it! Appreciate the insight and the link!
0

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

answered Jan 27, 2017 at 17:43

1 Comment

Isn't the JSON decoded already? At least, that's how I interpreted his first sentence.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.