关于获取api返回json的处理

WBOY
Release: 2016-06-06 20:21:38
Original
1710 people have browsed it

以下是api获取的数据,怎么获取到第一组数据呢?只要一组
也就是"20151224084":{"number":"9,7,6,8,0","dateline":"2015-12-24 20:02:55"}这个。

<code>{"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"}}</code>
Copy after login
Copy after login

回复内容:

以下是api获取的数据,怎么获取到第一组数据呢?只要一组
也就是"20151224084":{"number":"9,7,6,8,0","dateline":"2015-12-24 20:02:55"}这个。

<code>{"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"}}</code>
Copy after login
Copy after login

array_shift();

<code>$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;
}</code>
Copy after login

说明一下:第二种方式有个坑,就是当数组的长度特别长时,比如1000、10000,数组的翻转是一件很消耗性能的操作,所以,根据你的实际情况借鉴性的使用,第三种方式虽然笨,但是性能不错。
经友情提醒,刚才忘记了array_shift

json_decode($string, true)
转为数组后你肯定会了。

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!