2

Sorry if this is a duplication of anyone elses question but in my eyes it doesn't appear to match any other PHP multi dimensional array questions on here. And I've read them all. :-(

I'm trying to access values of this array (which I have no control over). But it's format is puzzling me and as yet I can't get at the values how I thought I should be able to.

This my JSON array example.

 {"Result":[{"Links":[{"UrlTo":"blahblahA/","Visited":1380104406,"FirstSeen":1375979058,"PrevVisited":1379817016,"Anchor":"text links","Type":"Text","Flag":[],"TextPre":"","TextPost":""}],"Index":0,"Rating":18.44400799929153,"UrlFrom":"blahblahB","IpFrom":"209.18.x.11","Title":"home page","LinksInternal":16,"LinksExternal":5,"Size":13107},{"Links":[{"UrlTo":"blahblahABC","Visited":1380574695,"FirstSeen":1375979100,"PrevVisited":1379224472,"Anchor":"text links","Type":"Text","Flag":[],"TextPre":"","TextPost":""}],"Index":1,"Rating":11.02675617091105,"UrlFrom":"blahblahXYZ","IpFrom":"209.18.x.11","Title":"Random title","LinksInternal":16,"LinksExternal":5,"Size":13107}]}

I naively thought that assigning this Json array to $arr Then decoding like so.

$arr = json_decode($str,true);

once done i could reference the array values like so...

$UrlTo = $arr['Result']['Links']['UrlTo'];

But I'm clearly not understanding this right. I've spent a lot of time googling, reading and still don't get it. I think it will be obvious to someone more experienced than I? Can someone help me understand the naivity of my approach.

3
  • 3
    Just put a print_r($arr) in your file to see the way the array is build Commented Oct 3, 2013 at 15:13
  • Does this work: $UrlTo = $arr['Result'][0]['Links'][0]['UrlTo'];? Commented Oct 3, 2013 at 15:13
  • [] stands for an array. [{}] is an object inside an array. Commented Oct 3, 2013 at 15:13

1 Answer 1

5

It's:

$arr['Result'][0]['Links'][0]['UrlTo'];

$arr['Result'] is an array, as is $arr['Result'][0]['Links']

To get the other result, do:

$arr['Result'][1]['Links'][0]['UrlTo'];
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for all comments and this answer from Frits was great. I was sure it was one of the variations I'd already tried but clearly not. after a bit of fiddling I worded out that some of the variables are accessed slightly differently. Would not have got there without this concept being explained to me though. Thank you. :-)

Your Answer

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.