Jump to content
ludo1960

Select specific keys from array

Recommended Posts

Posted (edited)

Hi guys,

I am getting data from a json source:

```

$jsondata = file_get_contents($url);
 
$response = json_decode($jsondata, true)
 
echo "<pre>"; print_r(array_keys($response));; echo "</pre>";

```

and this returns:

```

Array
(
    [0] => jsonapi
    [1] => data
    [2] => links
)

```

I want  to extract data from specific key in the data array (eg 'title'):

```

    [data] => Array
        (
            [0] => Array
                (
                    [type] => recipes
                    [id] => d6cd7643-6e44-4d26-9ed9-9da7196cef12
                    [attributes] => Array
                        (
                            [internalId] => 7
                            [isPublished] => 1
                            [title] => Deep mediterranean quiche
                            [createdAt] => 2019-05-07T13:34:27+0200
                            [updatedAt] => 2019-05-07T13:34:27+0200
                            [isPromoted] => 1
                            [path] => /recipes/deep-mediterranean-quiche
                            [cookingTime] => 30
                            [difficulty] => medium
                            [ingredients] => Array
                                (
                                    [0] => For the pastry:
                                    [1] => 280g plain flour
                                    [2] => 140g butter
                                    [3] => Cold water
                                    [4] => For the filling:
                                    [5] => 1 onion
                                    [6] => 2 garlic cloves
                                    [7] => Half a courgette
                                    [8] => 450ml soya milk
                                    [9] => 500g grated parmesan
                                    [10] => 2 eggs
                                    [11] => 200g sun dried tomatoes
                                    [12] => 100g feta
                                )

                            [numberOfservings] => 8
                            [preparationTime] => 40
                            [instructions] => 

```

How do I loop through the data array and select just the 'title' and 'difficulty' for example?

Edited by ludo1960

Share this post


Link to post
Share on other sites

Just use the index hierarchy down to the item you want

image.png.058b77d5e93ed5318c3f13352ebdfa6b.png

echo $array['data'][0]['attributes']['title']

 

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.