php从json数组获取字符串
朋友们,例如,我如何从下面的数组中获取“Clear”字符串,其中php中的dt=1553828400?提前谢谢 排列php从json数组获取字符串,php,arrays,json,Php,Arrays,Json,朋友们,例如,我如何从下面的数组中获取“Clear”字符串,其中php中的dt=1553828400?提前谢谢 排列 您可以通过文件获取内容()和基于您的条件的简单foreach()循环来实现,其中dt=1553828400 $response = json_decode(file_get_contents('https://samples.openweathermap.org/data/2.5/forecast/hourly?q=M%C3%BCnchen,DE&appid=439d4b
您可以通过
文件获取内容()
和基于您的条件的简单foreach()
循环来实现,其中dt=1553828400
$response = json_decode(file_get_contents('https://samples.openweathermap.org/data/2.5/forecast/hourly?q=M%C3%BCnchen,DE&appid=439d4b804bc8187953eb36d2a8c26a02'),true);
foreach($response['list'] as $key=>$value){
if($value['dt'] === 1553828400){
echo $value['weather'][0]['main'];
}
}
注意:您可以将
文件\u get\u contents()
替换为具有更多控制权的CURL
。解码到数组,从列表中提取dt
值作为键,然后通过键访问:
echo array_column(json_decode($json, true)['list'], null, 'dt')[1553828400]['weather'][0]['main'];
可重复使用以获取更多值:
$list = array_column(json_decode($json, true)['list'], null, 'dt');
echo $list[1553828400]['weather'][0]['main']; // Clear
echo $list[1553828400]['main']['temp']; // 276.465
非常感谢!!!很高兴它对你有所帮助。祝你好运,谢谢