-1

A remote PHP (that I do a php include with) file returns this data:

{"temp":"57.6","dev":"0.4","city":"Williamston","country":"United States","count":"72690"}

I want to take that data and load it into an array so I can do things with it, and I'm stuck with that part. :-) Any help would be appreciated!

I've tried different syntax as you see in what I'm trying to display.

<?php include("filename.php"); ?>
<?php
    function data() {
        $datalist = array(
           "currenttemp" => "temp",
           "deviation" => "dev",
           "last_processed"   => "city",
           "country" => "country",
       "stations_processed" => "count",
        ); } ?>


<table width="280" border="1">
<tbody>
<tr>
  <td>Current World Temp:</td>
  <td><?php echo $datalist[0]["temp"]; ?></td> 
</tr>
<tr>
  <td>Deviation:</td>
  <td><?php print "$deviation"; ?></td>
</tr>
<tr>
  <td>Last Station Processed:</td>
  <td><?php print "$last_processed"; ?>, <?php print "$country"; ?></td>
</tr>
<tr>
  <td>tations Processed Last Hour:</td>
  <td><?php print "$stations_processed"; ?></td>
</tr>
2
  • The data looks like a JSON string, so you can use json_decode(). Demo: 3v4l.org/GfOsQ Commented Dec 9, 2023 at 14:27
  • If you want an associative array rather than an object as the result, just set the 2nd argument to true. Demo: 3v4l.org/l5Ub6 . Docs: php.net/manual/en/function.json-decode.php Commented Dec 9, 2023 at 14:39

1 Answer 1

0

Just use json_decode and set the second parameter to true, so it will be converted into array:

json_decode('{"temp":"57.6","dev":"0.4","city":"Williamston","country":"United States","count":"72690"}', true)
Sign up to request clarification or add additional context in comments.

11 Comments

Still not quite following. This is the link to the data. temperature.global/api.php
It should go into a table like this <table width="280" border="1"> <tbody> <tr> <td>Current World Temp:</td> <td><?php print "$currenttemp"; ?></td> </tr> <tr> <td>Deviation:</td> <td><?php print "$deviation"; ?></td> </tr> <tr> <td>Last Station Processed:</td> <td><?php print "$last_processed"; ?>, <?php print "$country"; ?></td> </tr> <tr> <td>tations Processed Last Hour:</td> <td><?php print "$stations_processed"; ?></td> </tr> </tbody> </table>
@SteveBrueck what part are you unable to follow? Remember you haven't actually shown us how you get the data from the remote API, you've simply stated that you've done it. So we've started from that point. And we've shown you how to get the data into an array, which is what you asked. Outputting that data into a HTML table structure is the next step after that. You didn't ask about that. But there should be plenty of examples online showing the general process of getting data from an array into a HTML table using PHP. If you get stuck with that after you've tried, you can ask about it
I can get the data into the table if I can get it into an array. I was trying to use an php include that I stated in the original post. <?php include("temperature.global/api.php"); ?>
The api.php file returns this: {"temp":"57.6","dev":"0.4","city":"Williamston","country":"United States","count":"72690"} Of course the values returned will always be changing, but the field news remain the same. I need to convert that to an array. After that, I can do the rest.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.