Jump to content
#StayAtHome ×
branmh

JSON to PHP variables

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. 

Share this post


Link to post
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"]

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Putting stuff into variables doesn't matter. You have $json_ary - use that.

 

Why do you think you need variables?

Share this post


Link to post
Share on other sites

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.

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.