将json解码为php变量 [英] decode json into php variables

查看:174
本文介绍了将json解码为php变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JSON结果的内容转换为php变量.我正在使用的代码是:

I am trying to get the contents of a JSON result into php variables. The code I am using is:

$request = json_decode(file_get_contents("https://api.sunrise-sunset.org/json?lat=51.507351&lng=-0.127758&date=today"), true); 

JSON调用的输出如下所示:

The output of the JSON call looks like this:

array(2) { 
     ["results"]=> array(10) { 
         ["sunrise"]=> string(10) "4:21:35 AM" 
         ["sunset"]=> string(10) "7:32:34 PM" 
         ["solar_noon"]=> string(11) "11:57:04 AM" 
         ["day_length"]=> string(8) "15:10:59"
         ["civil_twilight_begin"]=> string(10) "3:42:08 AM"
         ["civil_twilight_end"]=> string(10) "8:12:01 PM"
         ["nautical_twilight_begin"]=> string(10) "2:49:55 AM" 
         ["nautical_twilight_end"]=> string(10) "9:04:14 PM" 
         ["astronomical_twilight_begin"]=> string(10) "1:41:12 AM" 
         ["astronomical_twilight_end"]=> string(11) "10:12:57 PM" 
      } 
      ["status"]=> string(2) "OK" 
} 

如何将日出"时间和日落"时间转换为php变量$ SunRiseTime和$ SunSetTime;

How can I get the "sunrise" time and the "sunset" times into php variables $SunRiseTime and $SunSetTime;

非常感谢您抽出宝贵的时间.

Many thanks in advance for your time.

推荐答案

简单而直接.

<?php
ini_set('display_errors', 1);

$json = file_get_contents("https://api.sunrise-sunset.org/json?lat=51.507351&lng=-0.127758&date=today"); 
$array=json_decode($json,true);

echo $SunRiseTime=$array["results"]["sunrise"];
echo $SunSetTime=$array["results"]["sunset"];

输出:4:21:35 AM 7:32:34 PM

这篇关于将json解码为php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆