2013-07-09 87 views
0

我有以下陣列把JSON結果到一個PHP數組

Array 
(
[responseData] => Array 
     (
     [results] => Array 
      (
       [0] => Array 
        (
         [GsearchResultClass] => GwebSearch 
         [unescapedUrl] => http://en.wikipedia.org/wiki/JH_(hash_function) 
         [url] => http://en.wikipedia.org/wiki/JH_(hash_function) 
         [visibleUrl] => en.wikipedia.org 
         [cacheUrl] => http://www.google.com/search?q=cache:jxOefvJSQXUJ:en.wikipedia.org 
         [title] => JH (hash function) - Wikipedia, the free encyclopedia 
         [titleNoFormatting] => JH (hash function) - Wikipedia, the free encyclopedia 
         [content] => JH is a cryptographic hash function submitted to the NIST hash function competition by Hongjun Wu. Though chosen as one of the five finalists of the competition ... 
        ) 

       [1] => Array 
        (
         [GsearchResultClass] => GwebSearch 
         [unescapedUrl] => http://www.jhaudio.com/ 
         [url] => http://www.jhaudio.com/ 
         [visibleUrl] => www.jhaudio.com 
         [cacheUrl] => http://www.google.com/search?q=cache:rO6NylpvTx8J:www.jhaudio.com 
         [title] => JH Audio: Custom In-Ear Monitors | In Ear Monitor 
         [titleNoFormatting] => JH Audio: Custom In-Ear Monitors | In Ear Monitor 
         [content] => Custom In-Ear Monitors by JHAudio - manufacturers of premium custom in ear monitors. JH Audio's products are a direct result of 25 years of live audio mixing ... 
        ) 

       [2] => Array 
        (
         [GsearchResultClass] => GwebSearch 
         [unescapedUrl] => http://www.jhaudio.com/collection/jha-pro-music-products 
         [url] => http://www.jhaudio.com/collection/jha-pro-music-products 
         [visibleUrl] => www.jhaudio.com 
         [cacheUrl] => http://www.google.com/search?q=cache:YY9q-E00yKkJ:www.jhaudio.com 
         [title] => JHA Pro Music Products | Custom In-Ear Monitors by JH Audio 
         [titleNoFormatting] => JHA Pro Music Products | Custom In-Ear Monitors by JH Audio 
         [content] => JHA Pro Music Products by JHAudio - manufacturers of premium custom in ear monitors. JH Audio's products are a direct result of 25 years of live audio mixing ... 
        ) 

       [3] => Array 
        (
         [GsearchResultClass] => GwebSearch 
         [unescapedUrl] => http://www.youtube.com/watch?v=JsQN3yZjRCI 
         [url] => http://www.youtube.com/watch%3Fv%3DJsQN3yZjRCI 
         [visibleUrl] => www.youtube.com 
         [cacheUrl] => http://www.google.com/search?q=cache:Dk4oETmQLNEJ:www.youtube.com 
         [title] => monta equina zagalo de j.h x gitana de la fortuna - YouTube 
         [titleNoFormatting] => monta equina zagalo de j.h x gitana de la fortuna - YouTube 
         [content] => Mar 5, 2010 ... caballos de exposicion en la modalidad de trote y galope, fecha de monta 1 de febrero de 2010.... 
        ) 

      ) 

我嘗試使用foreach循環解析它在PHP這樣的URL,標題和內容將被顯示。不過,我似乎無法正確解析代碼。下面的代碼給了我一個'responsedata'不被認可的錯誤。

foreach($json as value) 

echo $value [responsedata]; 

當我離開它回聲$價值,它給我的號碼200,這是responsestatus的價值。當我嘗試

foreach($json =>url => title => content as $value) 

不會承認的「=」號。

任何想法??我不是太熟悉JSON或php如果你沒有帶得到從後:)

TIA

回答

1

FTR,你貼什麼是土生土長的陣列,而不是一個「JSON數組」,試試這個:

$rawArray = get_results_somehow(); 

// Flatten the array to make it easier to work with 
$results = $rawArray['responseData']['results']; 

foreach ($results as $result) { 

    // Should now see the expected values 
    var_dump($result); 
} 
+0

你是否得到它的工作? –

1

該數據已經是數組形式。你需要像這樣迭代$ array ['responseData'] ['results']。

$arr = $array['responseData']['results']; 

foreach($arr as $k){ 
    echo $k['url']; 
    echo $k['title']; 
    echo $k['content']; 
}