Rascalsailor Posted July 29, 2022 Share Posted July 29, 2022 Hi all I have the following file of json data (json_file.json): { "name": "as.up.data.forward", "time": "2022-07-27T09:46:50.565402155Z", "identifiers": [{ "device_ids": { "device_id": "my_arduino-device", "application_ids": { "application_id": "mydevicename" }, "dev_eui": "012345678910", "join_eui": "109876543210", "dev_addr": "123456" } }] } I am using the following code to extract and display some of the data: <?php $json = file_get_contents('json_file.json'); // Decode the JSON file $json_data = json_decode($json,true); $identifiers_array = ($json_data['identifiers']); foreach($identifiers_array as $elem) { print_r($elem['device_ids']['application_ids']['application_id']); echo("<br/>"); print_r($elem['device_ids']['dev_eui']); echo("<br/>"); print_r($elem['device_ids']['device_id']); } ?> The output as expected, shows: mydevicename 012345678910 my_arduino-device My Question is: Can I not avoid the use of the foreach by doing something along the lines of: print_r($identifiers_array['device_ids']['application_ids']['application_id']); (can I not point to a specific element?) I'm really just wondering if I am following a 'best practice' method here. Thanks Russell Quote Link to comment https://forums.phpfreaks.com/topic/315103-do-i-need-foreach-for-accessing-specific-array-data-here/ Share on other sites More sharing options...
Barand Posted July 29, 2022 Share Posted July 29, 2022 1 hour ago, Rascalsailor said: (can I not point to a specific element?) Of course you can. foreach() is for iterating through all elements in an array. In this case you don't want to do that. $arr = json_decode($jsn, 1); $elem = $arr['identifiers'][0]['device_ids']; echo $elem['application_ids']['application_id'] . '<br>'; echo $elem['dev_eui'] . '<br>'; echo $elem['device_id'] . '<br>'; However, are you certain that the "identifiers" array will only have a single element? If the number is unknown, use foreach()... $arr = json_decode($jsn, 1); foreach ( $arr['identifiers'] as $ids ) { $elem = $ids['device_ids']; echo $elem['application_ids']['application_id'] . '<br>'; echo $elem['dev_eui'] . '<br>'; echo $elem['device_id'] . '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/315103-do-i-need-foreach-for-accessing-specific-array-data-here/#findComment-1598726 Share on other sites More sharing options...
Rascalsailor Posted July 29, 2022 Author Share Posted July 29, 2022 (edited) Great, thanks for the reply. The trick was to use: $elem = $arr['identifiers'][0]['device_ids']; That indexing : [0] was confusing me. regards Russell Edited July 29, 2022 by Rascalsailor Quote Link to comment https://forums.phpfreaks.com/topic/315103-do-i-need-foreach-for-accessing-specific-array-data-here/#findComment-1598730 Share on other sites More sharing options...
Barand Posted July 29, 2022 Share Posted July 29, 2022 Use echo '<pre>' . print_r($arr, 1) . '</pre>'; to give a readable formatted display of your array. Then follow the chain of keys... Quote Link to comment https://forums.phpfreaks.com/topic/315103-do-i-need-foreach-for-accessing-specific-array-data-here/#findComment-1598731 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.