简体   繁体   中英

Trying to access json array data with php

I am trying to reduce the amount of data returned from my php curl request to just that which is necessary but am struggling to access the data from the inner arrays/objects.

I'm currently receiving an error that the indexes within the $weatherdata are undefined.

Warning: Illegal string offset 'current' in projectname on line 21

This is the data that's returned:

{
  "lat": 33.44,
  "lon": -94.04,
  "timezone": "America/Chicago",
  "timezone_offset": -21600,
  "current": {
    "dt": 1618317040,
    "sunrise": 1618282134,
    "sunset": 1618333901,
    "temp": 284.07,
    "feels_like": 282.84,
    "pressure": 1019,
    "humidity": 62,
    "dew_point": 277.08,
    "uvi": 0.89,
    "clouds": 0,
    "visibility": 10000,
    "wind_speed": 6,
    "wind_deg": 300
 }
    "daily": [
    {
      "dt": 1618308000,
      "sunrise": 1618282134,
      "sunset": 1618333901,
      "moonrise": 1618284960,
      "moonset": 1618339740,
      "moon_phase": 0.04,
      "temp": {
        "day": 279.79,
        "min": 275.09,
        "max": 284.07,
        "night": 275.09,
        "eve": 279.21,
        "morn": 278.49
      },

etc

And then my current code in php:

<?php


    $url='https://api.openweathermap.org/data/2.5/onecall?lat=' . $_REQUEST['lat'] . '&lon=' . $_REQUEST['lng'] . '&units=metric&appid=foo';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);

    $result=curl_exec($ch);

    curl_close($ch);

    $decode = json_decode($result,true);
    
    $countryData = [];

    foreach($decode as $weatherdata){
        $countryinfo = [];
        $countryinfo['uv'] = $weatherdata['current']['uvi'];
        $countryinfo['todaytemp'] = $weatherdata['daily'][0]['temp']['day'];
          
        array_push($countryData, $countryinfo);
    };

    $output['status']['code'] = "200";
    $output['status']['name'] = "ok";
    $output['status']['description'] = "success";
    $output['data'] = $countryData;


    echo json_encode($output); 

?>

Any guidance would be appreciated! Thanks

Your foreach is causing this error. You are looping through the resulting array

在此处输入图像描述

When you decode the above json using json_decode($result, true) , the resulting array would be like

在此处输入图像描述

 foreach($decode as $weatherdata){
// On first Iteration, $weatherdata will be "lat":33.44,
// On Second Iteration, $weatherdata will be "lon":-94.04,
// On Third Iteration, $weatherdata will be "timezone":"America/Chicago",
}

so if you try to access $weatherdata['current']['uvi'] ; You will get undefined indexes error

To get the UVI, You have to call

$uvi = $decode['current']['uvi'];

To get today's temperature from daily

$todaysTemp = $decode['daily'][0]['temp']['day'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM