Jump to content

Recommended Posts

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. 

Link to comment
https://forums.phpfreaks.com/topic/302933-json-to-php-variables/
Share on other sites

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"]

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 by Zyx

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?

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.