Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php从json数组获取字符串_Php_Arrays_Json - Fatal编程技术网

php从json数组获取字符串

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

朋友们,例如,我如何从下面的数组中获取“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=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

非常感谢!!!很高兴它对你有所帮助。祝你好运,谢谢