从数组中读取数据

时间:2016-04-13 18:50:49

标签: php arrays json

通过调用我将数据返回到数组(JSON)中。我用:

$jsonurl = "https://api.openarch.nl/1.0/records/search.json?name=baltus&lang=en&number_show=5&archive=rhl&start=0";
$jsondata = file_get_contents($jsonurl);
$json = json_decode($jsondata,true);

$i=0;
foreach ($json as $item)
{
    print_r($item)[$i];
$i++;   
}

我可以打印外部数组,但是我不能遍历内部[docs]数组以获取数据显示在表中并使用它来填充变量。

Array 
( 
[query] => Array ( [archive] => Regionaal Historisch Centrum Limburg [name] => baltus [only_results_with_scans] => [start] => 0 [number_show] => 5 [sort] => 1 [language] => en ) 

[response] => Array ( 
    [number_found] => 1388 
    [docs] => Array (
        [0] => Array ( [identifier] => 90cbe7d7-1eca-5654-feb5-d397f11b0412 [archive] => Regionaal Historisch Centrum Limburg [personname] => Adrian Baltus van Tienhoven [relationtype] => Vader [eventtype] => Geboorte [eventdate] => Array ( [day] => 19 [month] => 11 [year] => 1891 ) [eventplace] => Venlo [sourcetype] => BS Geboorte ) 
        [1] => Array ( [identifier] => 93a07a53-9b80-9d14-196b-be30a6e17c21 [archive] => Regionaal Historisch Centrum Limburg [personname] => Adrianus Baltus van Tienhoven [relationtype] => Vader [eventtype] => Geboorte [eventdate] => Array ( [day] => 26 [month] => 9 [year] => 1893 ) [eventplace] => Venlo [sourcetype] => BS Geboorte ) 
        [2] => Array ( [identifier] => cb92d489-228a-dc8b-6a04-3273452b4994 [archive] => Regionaal Historisch Centrum Limburg [personname] => Adrianus Baltus van Tienhoven [relationtype] => Vader [eventtype] => Overlijden [eventdate] => Array ( [day] => 24 [month] => 11 [year] => 1891 ) [eventplace] => Venlo [sourcetype] => BS Overlijden ) 
        [3] => Array ( [identifier] => 2ef73050-76ac-362b-213d-1cab5a1199f2 [archive] => Regionaal Historisch Centrum Limburg [personname] => Agnes Baltus [relationtype] => Moeder [eventtype] => Overlijden [eventdate] => Array ( [day] => 27 [month] => 1 [year] => 1869 ) [eventplace] => Posterholt [sourcetype] => BS Overlijden ) 
        [4] => Array ( [identifier] => 3c8dc193-4753-172d-2935-24a2f2162732 [archive] => Regionaal Historisch Centrum Limburg [personname] => Alexander Baltus [relationtype] => Vader van de bruidegom [eventtype] => Huwelijk [eventdate] => Array ( [day] => 19 [month] => 7 [year] => 1842 ) [eventplace] => Beek [sourcetype] => BS Huwelijk ) ) ) ) 

1 个答案:

答案 0 :(得分:1)

循环嵌套数组并访问该数组中的所需键:

foreach($json['response']['docs'] as $item) {
    echo $item['identifier'];
    // etc...
}