我正在使用json_decode从API访问数据。我的代码返回所有日期的数组(见下文),但我想返回特定数据,如'name'或'locale'。
$json_string = 'http://api.duedil.com/open/search?q=Surfing%20Sumo&api_key=THE-API-KEY';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
echo '<pre>';
var_dump($obj);
这是返回的内容(这里简称为节省空间):
array(1) {
["response"]=>
array(2) {
["pagination"]=>
string(79) "http://api.duedil.com/open/search?query=Duedil&total_results=6&limit=5&offset=5"
["data"]=>
array(5) {
[0]=>
array(4) {
["company_number"]=>
string(8) "06999618"
["locale"]=>
string(14) "United Kingdom"
["name"]=>
string(14) "Duedil Limited"
["uri"]=>
string(51) "http://api.duedil.com/open/uk/company/06999618.json"
}
答案 0 :(得分:1)
你可以使用
$name = $obj['response']['data'][0]['name'];
$locale = $obj['response']['data'][0]['locale'];
如果你有多个返回值,你可以循环它们
foreach ($obj['response']['data'] as $item) {
$name = $item['name'];
$locale = $item['locale'];
}
答案 1 :(得分:1)
试试这个示例代码:
<?php
$data = isset($obj['response']['data'])?$obj['response']['data']:FALSE;
if(is_array($data))
{
foreach ($data as $value) {
echo $value['name'];
echo $value['locale'];
}
}