Skip to content
Advertisement

Access JSON Array Data Within Object With PHP

JavaScript

I have this JSON object and array in PHP that I have decoded.

I am trying to access ‘This Value’ but I am stuck.

I also need to find a way to do it where I don’t know if it will always be number 6.

I have made these attempts:

JavaScript

and

JavaScript

I can access the email like this:

JavaScript

Advertisement

Answer

You will need to search through the subarray for the qualifying object using the key property’s value.

There will be functional techniques to do this, but I’ll show a simple loop and break technique.

Code: (Demo)

JavaScript

Output:

JavaScript

A functional search can look like this: (Demo)

JavaScript

If you want simpler access to multiple values in the subarray, then assign temporary keys: (Demo)

JavaScript

Output:

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