I'm pulling some JSON from an API, and then converting it to a standard PHP array. The structure, once converted, looks like:
Array
(
[0] => stdClass Object
(
[age_range] => over 34
[outcome] => A no further action disposal
[involved_person] => 1
[self_defined_ethnicity] => White - English/Welsh/Scottish/Northern Irish/British
[gender] => Male
[legislation] =>
[outcome_linked_to_object_of_search] =>
[datetime] => 2023-02-20T08:42:00+00:00
[removal_of_more_than_outer_clothing] =>
[outcome_object] => stdClass Object
(
[id] => bu-no-further-action
[name] => A no further action disposal
)
[location] => stdClass Object
(
[latitude] => 51.305042
[street] => stdClass Object
(
[id] => 2006554
[name] => On or near Petrol Station
)
[longitude] => -0.410448
)
[operation] =>
[officer_defined_ethnicity] => White
[type] => Person and Vehicle search
[operation_name] =>
[object_of_search] => Anything to threaten or harm anyone
)
[1] => stdClass Object
(
[age_range] => 18-24
[outcome] => A no further action disposal
[involved_person] => 1
[self_defined_ethnicity] => White - English/Welsh/Scottish/Northern Irish/British
[gender] => Male
[legislation] =>
[outcome_linked_to_object_of_search] =>
[datetime] => 2023-02-04T18:13:00+00:00
[removal_of_more_than_outer_clothing] =>
[outcome_object] => stdClass Object
(
[id] => bu-no-further-action
[name] => A no further action disposal
)
[location] => stdClass Object
(
[latitude] => 51.411582
[street] => stdClass Object
(
[id] => 2003522
[name] => On or near Staines Road
)
[longitude] => -0.509454
)
[operation] =>
[officer_defined_ethnicity] => White
[type] => Person and Vehicle search
[operation_name] =>
[object_of_search] => Anything to threaten or harm anyone
)
etc etc
However, whilst I can extract most values, I can't seem to pull the lower nested data under [street].
So I'm basically looping through the array... so I can do something like:
$response_data = json_decode($json_data);
$i = 0;
while($i < count($response_data)) {
echo $response_data[$i]->gender;
echo $response_data[$i]->age_range;
$i++;
}
That works fine for level 1 data.
For the next level of nested data I can use something like:
echo $response_data[$i]->location->latitude;
But when I try and grab a lower nested bit of data, it doesn't seem to work. The following dones't work (as in no value is returned), and I don't understand why?
echo $response_data[$i]->location->street->name;
I'm not that familiar with PHP arrays, so I'm probably doing something stupid, but can anyone point me in the right direction?