-1

I'm just trying to get into PHP and understand the core concepts, so have been messing around with arrays, jsons, etc. For this, I've tried everything, yet I cannot get to access the "Name" component of my Json. I recieve it from a CURL HTTP Request, decode it from a json then attempt to go from there. The JSON looks like:

[
  {
    "food1": "blah",
    "food2": "blah",
    "food3": "blah"
  },
  {
    "food1": "blah",
    "data": [

      {
        "name": "I need to access this",
        "name2": "Another name"
      }
    ]
  }
]

I've tried $Result[0]["data"]["name"], $result[0][1]["data"]["name"] and plenty more combinations of that kind of nature, yet still haven't been able to figure it out. Any help would be greatly appreciated, thanks. :)

1 Answers1

-1

In your specific example you'd use $array[1]['data'][0]['name']

A breakdown of your array:

[
  // this is the 0th element
  {
    "food1": "blah",
    "food2": "blah",
    "food3": "blah"
  },
  
  // this is the 1st element
  {
    "food1": "blah",

    // 1st element's data property
    "data": [

      // 1st element's data property's 0th element
      {

        // [1]st element's [data] property's [0]th element's [name] property
        "name": "I need to access this",
        "name2": "Another name"
      }
    ]
  }
]
Gavin
  • 2,214
  • 2
  • 18
  • 26
  • 1
    We get these types of questions every week. We don't need to answer these. Please close questions that ask "how to access json" or "how to access array/object" as a duplicate. They are all snowflakes, but well covered by canonical pages. – mickmackusa Jun 13 '22 at 07:59