ludo1960 Posted May 12, 2019 Share Posted May 12, 2019 (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 May 12, 2019 by ludo1960 Quote Link to comment https://forums.phpfreaks.com/topic/308698-select-specific-keys-from-array/ Share on other sites More sharing options...
Barand Posted May 12, 2019 Share Posted May 12, 2019 Just use the index hierarchy down to the item you want echo $array['data'][0]['attributes']['title'] 1 Quote Link to comment https://forums.phpfreaks.com/topic/308698-select-specific-keys-from-array/#findComment-1566618 Share on other sites More sharing options...
ludo1960 Posted May 12, 2019 Author Share Posted May 12, 2019 Perfect, thanks Barand. Quote Link to comment https://forums.phpfreaks.com/topic/308698-select-specific-keys-from-array/#findComment-1566619 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.