0

I try to use api.openweathermap.org for get current weather. I use a php request to get a data. I sucessfully receive data and convert it to array with objects. I have a small question: How to get a value of key named "MAIN", which is equal to "Snow"? I try to use api.openweathermap.org for get current weather. I use a php request to get a data. I sucessfully receive data and convert it to array with objects. I have a small question: How to get a value of key named "MAIN", which is equal to "Snow"?

object(stdClass)#3 (14) {
  ["coord"]=>
  object(stdClass)#1 (2) {
    ["lon"]=>
    float(30.2642)
    ["lat"]=>
    float(59.8944)
  }
  ["weather"]=>
  array(1) {
    [0]=>
    object(stdClass)#4 (4) {
      ["id"]=>
      int(600)
      ["main"]=>
      string(4) "Snow"
      ["description"]=>
      string(27) "small snow"
      ["icon"]=>
      string(3) "13n"
    }
  }
  ["base"]=>
  string(8) "stations"
  ["main"]=>
  object(stdClass)#5 (6) {
    ["temp"]=>
    float(-11.94)
    ["feels_like"]=>
    float(-18.94)
    ["temp_min"]=>
    float(-15.2)
    ["temp_max"]=>
    float(-11.08)
    ["pressure"]=>
    int(1010)
    ["humidity"]=>
    int(86)
  }
  ["visibility"]=>
  int(10000)
  ["wind"]=>
  object(stdClass)#6 (2) {
    ["speed"]=>
    int(4)
    ["deg"]=>
    int(100)
  }
  ["snow"]=>
  object(stdClass)#7 (1) {
    ["1h"]=>
    float(0.5)
  }
  ["clouds"]=>
  object(stdClass)#8 (1) {
    ["all"]=>
    int(100)
  }
  ["dt"]=>
  int(1641652836)
  ["sys"]=>
  object(stdClass)#9 (5) {
    ["type"]=>
    int(2)
    ["id"]=>
    int(197864)
    ["country"]=>
    string(2) "RU"
    ["sunrise"]=>
    int(1641624902)
    ["sunset"]=>
    int(1641647737)
  }
  ["timezone"]=>
  int(10800)
  ["id"]=>
  int(498817)
  ["name"]=>
  string(29) "SPb"
  ["cod"]=>
  int(200)
}
1
  • This isn't an array, it's an object. I recommend having a look at some introductory PHP tutorials, which will show you how to get properties of an object. Commented Jan 8, 2022 at 15:08

1 Answer 1

1

You could get the value like this :

$weatherType = $weather->weather[0]->main;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.