关于获取api返回json的处理
程序员文章站
2024-01-08 12:14:16
...
以下是api获取的数据,怎么获取到第一组数据呢?只要一组
也就是
也就是
"20151224084":{"number":"9,7,6,8,0","dateline":"2015-12-24 20:02:55"}
这个。
{"20151224084":{"number":"9,7,6,8,0","dateline":"2015-12-24 20:02:55"},"20151224083":{"number":"4,2,7,5,1","dateline":"2015-12-24 19:52:55"},"20151224082":{"number":"6,5,5,5,7","dateline":"2015-12-24 19:43:36"},"20151224081":{"number":"3,8,6,0,8","dateline":"2015-12-24 19:33:01"},"20151224080":{"number":"5,1,6,7,9","dateline":"2015-12-24 19:23:01"},"20151224079":{"number":"3,8,0,3,5","dateline":"2015-12-24 19:13:05"},"20151224078":{"number":"1,2,2,8,4","dateline":"2015-12-24 19:02:57"},"20151224077":{"number":"1,4,9,1,1","dateline":"2015-12-24 18:52:58"},"20151224076":{"number":"3,2,4,0,3","dateline":"2015-12-24 18:43:25"},"20151224075":{"number":"0,3,2,9,7","dateline":"2015-12-24 18:32:59"}}
回复内容:
以下是api获取的数据,怎么获取到第一组数据呢?只要一组
也就是"20151224084":{"number":"9,7,6,8,0","dateline":"2015-12-24 20:02:55"}
这个。
{"20151224084":{"number":"9,7,6,8,0","dateline":"2015-12-24 20:02:55"},"20151224083":{"number":"4,2,7,5,1","dateline":"2015-12-24 19:52:55"},"20151224082":{"number":"6,5,5,5,7","dateline":"2015-12-24 19:43:36"},"20151224081":{"number":"3,8,6,0,8","dateline":"2015-12-24 19:33:01"},"20151224080":{"number":"5,1,6,7,9","dateline":"2015-12-24 19:23:01"},"20151224079":{"number":"3,8,0,3,5","dateline":"2015-12-24 19:13:05"},"20151224078":{"number":"1,2,2,8,4","dateline":"2015-12-24 19:02:57"},"20151224077":{"number":"1,4,9,1,1","dateline":"2015-12-24 18:52:58"},"20151224076":{"number":"3,2,4,0,3","dateline":"2015-12-24 18:43:25"},"20151224075":{"number":"0,3,2,9,7","dateline":"2015-12-24 18:32:59"}}
array_shift();
$json = '{"20151224084":{"number":"9,7,6,8,0","dateline":"2015-12-24 20:02:55"},"20151224083":{"number":"4,2,7,5,1","dateline":"2015-12-24 19:52:55"},"20151224082":{"number":"6,5,5,5,7","dateline":"2015-12-24 19:43:36"},"20151224081":{"number":"3,8,6,0,8","dateline":"2015-12-24 19:33:01"},"20151224080":{"number":"5,1,6,7,9","dateline":"2015-12-24 19:23:01"},"20151224079":{"number":"3,8,0,3,5","dateline":"2015-12-24 19:13:05"},"20151224078":{"number":"1,2,2,8,4","dateline":"2015-12-24 19:02:57"},"20151224077":{"number":"1,4,9,1,1","dateline":"2015-12-24 18:52:58"},"20151224076":{"number":"3,2,4,0,3","dateline":"2015-12-24 18:43:25"},"20151224075":{"number":"0,3,2,9,7","dateline":"2015-12-24 18:32:59"}}';
$data = json_decode($json,true);
// 方法1
$want = $data['20151224084'];
// 方法2
$want = array_pop(array_reverse($data,true));
// 最笨的办法
$want = [];
foreach($data as $key=>$value){
$want = [$key=>$value];
break;
}
说明一下:第二种方式有个坑,就是当数组的长度特别长时,比如1000、10000,数组的翻转是一件很消耗性能的操作,所以,根据你的实际情况借鉴性的使用,第三种方式虽然笨,但是性能不错。
经友情提醒,刚才忘记了array_shift
json_decode($string, true)
转为数组后你肯定会了。
上一篇: PHP递归实现无限级分门别类
推荐阅读
-
【原创】微信授权、获取用户openid-纯前端实现——jsonp跨域访问返回json数据会报错的纯前端解决办法
-
系统中 部分 请求是ajax是用api接口还是直接处理就返回json
-
基于JSON类型返回的API接口如果做输出校验?
-
关于淘宝API返回XML简单对象提取为数组的有关问题
-
关于获取api返回json的处理
-
PHP form提交到某API地址后,接收返回的json的问题
-
PHP处理Json字符串解码返回NULL的解决方法,jsonnull
-
API接口调用并处理返回的json数据
-
MySQL 5.7 关于 JSON 类型的子集Key/Value获取方法分享
-
利用json返回的数据获取值,在jquery里面写着没有解析出来