Массив

Shepard

Активный
Автор темы
459
88
Мне приходит этот текст
Код:
{"coord":{"lon":28.5815,"lat":49.894},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":285.6,"feels_like":284.52,"temp_min":285.6,"temp_max":285.6,"pressure":1013,"humidity":62,"sea_level":1013,"grnd_level":984},"visibility":10000,"wind":{"speed":2.03,"deg":12,"gust":2.68},"clouds":{"all":100},"dt":1682869093,"sys":{"country":"UA","sunrise":1682822692,"sunset":1682875249},"timezone":10800,"id":712441,"name":"Berdychiv","cod":200}

PHP:
        $ch = curl_init('https://api.openweathermap.org/data/2.5/weather?lat=49.8940442&lon=28.5814912&appid=88e7a5ce572bcf73485e8ba74418461');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

        $response = curl_exec($ch);
        curl_close($ch);

        var_dump($response);
        $tg->sendMessage($chat_id, $response['weather']['main'], null, false, null, $reply_markup);
Я хочу выводить в конце конкретные значения, типу ['weather']['main'], но оно не в массиве. Как мне его запихнуть в него либо я не так использую?
 

norrthh

Известный
280
102
PHP:
// Декодируем JSON-ответ в ассоциативный массив
$responseArray = json_decode($response, true);

// Получаем значение "main" из массива
$weatherMain = $responseArray['weather'][0]['main'];

// Выводим значение "main"
echo $weatherMain;
Здесь мы сначала декодируем JSON-ответ в переменную $responseArray, затем получаем значение "main" из массива $responseArray['weather'][0],и, наконец, выводим значение "main" с помощью echo.