2016-10-31 13 views
1

JSONの配列結果に問題があります。これは私のjsonファイルです、私は各オブジェクトのすべての "extlinks"結果配列を抽出したいと思います。どんな奉仕ですか?これはウィキペディアAPI jsonです。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アレイをエコーできますか?

+2

JSONは無関係です:

<?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>"; 

これが出力されます。それをネイティブのPHPオブジェクト/配列構造体にデコードしていて、他のネストされたPHPオブジェクト/配列構造体のようにアクセスします。 –

答えて

0

あなたは、このようにそれを達成することができます

 
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 
) 
+0

これはオリジナルのjsonですhttps://en.wikipedia.org/w/api.php?action=query&titles=rome&prop=extlinks&format=json –

+0

@EVEMilano then works;) – nanocv

+0

ありがとう、これは動作しますが、各オブジェクトの結果鎖が張られている。 Json内では、extlinksの結果は連続しているか、連鎖しています。あなたは1行に1つを得ることが可能だと思いますか? –

関連する問題