0

I'm trying to learn how to use PHP arrays.

I have basically created a script that interogates an API, gets the resulting JSON data and then converts that into a PHP array.

The final array looks like:

Array
(
    [0] => stdClass Object
        (
            [age_range] => 
            [outcome] => A no further action disposal
            [involved_person] => 1
            [self_defined_ethnicity] => Other ethnic group - Not stated
            [gender] => Male
            [legislation] => 
            [outcome_linked_to_object_of_search] => 
            [datetime] => 2023-05-17T19:56: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.459157
                    [street] => stdClass Object
                        (
                            [id] => 2004699
                            [name] => On or near Parking Area
                        )

                    [longitude] => -0.478454
                )

            [operation] => 
            [officer_defined_ethnicity] => White
            [type] => Person search
            [operation_name] => 
            [object_of_search] => Controlled drugs
        )

    [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] => 1
            [datetime] => 2023-05-12T22:19: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.434468
                    [street] => stdClass Object
                        (
                            [id] => 2003771
                            [name] => On or near Matthews Lane
                        )

                    [longitude] => -0.505257
                )

            [operation] => 
            [officer_defined_ethnicity] => White
            [type] => Person search
            [operation_name] => 
            [object_of_search] => Controlled drugs
        )

etc

I now basically want to loop through this array and for each entry grab [gender], [datetime], ]latitude], [longitude] and [officer_defined_ethnicity]

I have very little experience with PHP arrays, but I understand that I should be able to pull individual values using something like:

$array[0]["officer_defined_ethnicity"]

I could then wrap this into a loop.

But if I do something like:

$response_data = json_decode($json_data);
echo $response_data[0]["officer_defined_ethnicity"];

I just get an error.

Sorry if this is really basic, but I'm struggling to get a foothold as I can't even seemingly output single values.

Damian
  • 1
  • 1
  • Add `true` as second argument to `json_decode`: `json_decode($json_data, true)`. Otherwise it parses JSON objects as PHP objects and you'd have to access their properties: `$response_data[0]->officer_defined_ethnicity;` – Martin Heralecký Jul 07 '23 at 09:41

0 Answers0