我有一些问题,数组结果是JSON。这是我的json文件,我想为每个对象提取所有“extlinks”结果Arrays。任何服务?这是维基百科API json。
Array
(
[continue] => Array
(
[eloffset] => 10
[continue] => ||
)
[query] => Array
(
[normalized] => Array
(
[0] => Array
(
[from] => rome
[to] => Rome
)
)
[pages] => Array
(
[25458] => Array
(
[pageid] => 25458
[ns] => 0
[title] => Rome
[extlinks] => Array
(
[0] => Array
(
[*] => //dx.doi.org/10.1017%2FS0009840X00221331
)
[1] => Array
(
[*] => //dx.doi.org/10.2307%2F295257
)
[2] => Array
(
[*] => //tools.wmflabs.org/geohack/geohack.php?pagename=Rome¶ms=41_54_N_12_30_E_type:city_region:IT
)
[3] => Array
(
[*] => //web.archive.org/web/20071210175055/http://english.seoul.go.kr/gover/cooper/coo_02sis.html
)
[4] => Array
(
[*] => //web.archive.org/web/20080204030918/http://www.romaperkyoto.org/index.php?option=com_content&task=view&id=35&Itemid=52
)
[5] => Array
(
[*] => //web.archive.org/web/20080508191341/http://www.commune-tunis.gov.tn/fr/mairie_cooperation1.htm
)
[6] => Array
(
[*] => //web.archive.org/web/20080530094628/http://www.trincoll.edu/depts/rome/curriculum/rome350.html
)
[7] => Array
(
[*] => //web.archive.org/web/20080613192334/http://www.mpg.de/english/aboutTheSociety/aboutUs/scientificAwards/awardsOfMPS/hannoIlseHahnPrize/index.html
)
[8] => Array
(
[*] => //web.archive.org/web/20080708234610/http://www.isvroma.it/public/EN/index.php?option=com_content&task=view&id=13&Itemid=+
)
[9] => Array
(
[*] => //web.archive.org/web/20130702010825/http://www.krakow.pl/otwarty_na_swiat/2531,kat,0,5,miasta_partnerskie.html
)
)
)
)
)
)
我的PHP代码是:
$str=file_get_contents($url);
$json = json_decode($str, true);
我如何回应所有的extlinks数组?
答案 0 :(得分:0)
你可以这样做:
<?php
$url = "https://en.wikipedia.org/w/api.php?action=query&titles=rome&prop=extlinks&format=json";
$str=file_get_contents($url);
$json = json_decode($str, true);
$linksArray = [];
// current() takes an array and returns the current value (first in this case)
foreach (current($json['query']['pages'])['extlinks'] as $extlink){
$pos = strpos($extlink['*'], 'http');
if($pos == false){
$linksArray[] = $extlink['*'];
}
else{
$linksArray[] = substr($extlink['*'], 0, $pos);
$linksArray[] = substr($extlink['*'], $pos);
}
}
echo "<pre>";
print_r($linksArray);
echo "</pre>";
这是输出:
Array ( [0] => //dx.doi.org/10.1017%2FS0009840X00221331 [1] => //dx.doi.org/10.2307%2F295257 [2] => //tools.wmflabs.org/geohack/geohack.php?pagename=Rome¶ms=41_54_N_12_30_E_type:city_region:IT [3] => //web.archive.org/web/20071210175055/ [4] => http://english.seoul.go.kr/gover/cooper/coo_02sis.html [5] => //web.archive.org/web/20080204030918/ [6] => http://www.romaperkyoto.org/index.php?option=com_content&task=view&id=35&Itemid=52 [7] => //web.archive.org/web/20080508191341/ [8] => http://www.commune-tunis.gov.tn/fr/mairie_cooperation1.htm [9] => //web.archive.org/web/20080530094628/ [10] => http://www.trincoll.edu/depts/rome/curriculum/rome350.html [11] => //web.archive.org/web/20080613192334/ [12] => http://www.mpg.de/english/aboutTheSociety/aboutUs/scientificAwards/awardsOfMPS/hannoIlseHahnPrize/index.html [13] => //web.archive.org/web/20080708234610/ [14] => http://www.isvroma.it/public/EN/index.php?option=com_content&task=view&id=13&Itemid=+ [15] => //web.archive.org/web/20130702010825/ [16] => http://www.krakow.pl/otwarty_na_swiat/2531,kat,0,5,miasta_partnerskie.html )