branmh Posted January 15, 2017 Share Posted January 15, 2017 I new to using JSON and PHP. I need some help on how to convert it over to a php array of variables. I want to take this file https://api.weather.gov/stations/KAMA/observations/current and convert it over. Is it possible to put everything into variables? I'm able to see the entire json with this code $json_ary = json_decode($file,true); foreach($json_ary as $key => $val) { //echo "$key<br>\n"; foreach ($val as $key2 => $val2) { echo "... $key2 => $val2<br>\n"; foreach ($val2 as $key3 => $val3) { echo "... $key3 => $val3<br>\n"; } } } But it's not returning what I'm wanting. Like on temperature, I would like the value to automatically assigned to temperature. If you can help that would be greatly appreciated. At the beginning of the week I lost my friend that helps me out on this stuff and I'm so lost. Quote Link to comment https://forums.phpfreaks.com/topic/302933-json-to-php-variables/ Share on other sites More sharing options...
requinix Posted January 15, 2017 Share Posted January 15, 2017 You cannot foreach over it just because it is an array. It doesn't make sense to do that with this data. If you want specific data then you need to get it specifically. For example, the temperature is at $json_ary["properties"]["temperature"]["value"] Quote Link to comment https://forums.phpfreaks.com/topic/302933-json-to-php-variables/#findComment-1541398 Share on other sites More sharing options...
Zyx Posted January 15, 2017 Share Posted January 15, 2017 (edited) Moreover, extracting variables from arrays fetched from external sources poses a threat to the security of the application. What if someone puts a key into an array that overwrites an existing variable belonging to your application? You can repack it to use it more conveniently, but always keep these data in some array or object. $json_ary = json_decode($file, true); $weatherInfo = array( 'temperature' => $json_ary['properties']['temperature']['value'], ); echo $weatherInfo['temperature']; Edited January 15, 2017 by Zyx Quote Link to comment https://forums.phpfreaks.com/topic/302933-json-to-php-variables/#findComment-1541399 Share on other sites More sharing options...
branmh Posted January 15, 2017 Author Share Posted January 15, 2017 Thanks, with the key it would have another name in front of it to prevent overwrites. But there isn't a way to save everything to variables? So how would I be able to use https://api.weather.gov/stations/KAMA/observations, when it as multiple nodes named the same but different data? Quote Link to comment https://forums.phpfreaks.com/topic/302933-json-to-php-variables/#findComment-1541407 Share on other sites More sharing options...
requinix Posted January 15, 2017 Share Posted January 15, 2017 Putting stuff into variables doesn't matter. You have $json_ary - use that. Why do you think you need variables? Quote Link to comment https://forums.phpfreaks.com/topic/302933-json-to-php-variables/#findComment-1541408 Share on other sites More sharing options...
branmh Posted January 15, 2017 Author Share Posted January 15, 2017 I have to assign variables in my php fie to use in my template. Quote Link to comment https://forums.phpfreaks.com/topic/302933-json-to-php-variables/#findComment-1541411 Share on other sites More sharing options...
Jacques1 Posted January 15, 2017 Share Posted January 15, 2017 Simply pass the entire $json_ary (you might want to choose a better name) to your template and then access whatever data you need. I also strongly recommend you learn the basics of PHP before jumping into this project. Otherwise you'll waste a lot of time on imaginary problems like this one and wrong approaches like in your other thread. Quote Link to comment https://forums.phpfreaks.com/topic/302933-json-to-php-variables/#findComment-1541413 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.