-1

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?

Damo
  • 1
  • 1
  • Please extract and provide a [mcve], not some snippets of code which people need to guess how they form a whole. As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Jul 07 '23 at 20:32
  • Sometimes it can help to [temporarily switch on error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – KIKO Software Jul 07 '23 at 20:40
  • Although this isn't your problem. You can pass a second parameter to **json_decode($json_data,true);**. By passing true, it converts the JSON to an associative array instead of an object. You can then loop like **foreach($response_data as $row)**. – imvain2 Jul 07 '23 at 20:48
  • Would be helpfull if i have the data, but appears: echo $response_data[$i]->location->['street']->name; – Matheus Brito Jul 07 '23 at 21:07

1 Answers1

-2

This was me being silly.

The data pull was working fine, but some of the values included apostrophes that were messing up my Javascript and preventing data from loading. I just used addslashes() to fix.

Damo
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 10 '23 at 22:06