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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

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.