Skip to content
Advertisement

php json parse access child values

I’m trying to filter and later access to the child data of a nested json file using PHP but for now i’m unable to do it.

Json example:

JavaScript

I’ve tried with this code:

JavaScript

So $domain_id is an external value that I passed through the function. The idea is that if it matches one of the child’s values, then I can extract the values inside that child.

If I pass $domain_id as Zf6Yr2n07 then I need to extract the TXT information (name) inside that child node. e.g. _amazonses2.test.com

Any tip will be highly appreciated.

Advertisement

Answer

First, your JSON is invalid. Make sure to remove trailing commas.

Second, you’re accessing $item["uuid"]["dns_records"]["name"], but

  1. dns_records is an array
  2. dns_records exists on $item, not $item["uuid"]

To get only the first name, just select the first element inside the dns_records array: $item["dns_records"][0]["name"]

Another option is to return an array with all the available names by looping through all child elements of dns_records and pushing the name to a new array:

JavaScript

Live demo on onlinephp.io

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement