2014-01-15 116 views
-2

我想從json中提取一些數據,然後卡住了。在JSON這裏開始:從json中提取php數據

{torrent: "",rss: {channel: {title: "The Pirate Bay - TV shows",link: "test"}}} 

,在這裏我的代碼:

<?php 
    require_once 'rss_php.php';  
    $rss = new rss_php; 
    $rss->load('http://rss.thepiratebay.se/205'); 
    $jsonData = json_encode($rss->getRSS()); 
    $phpArray = json_decode($jsonData); 
    foreach ($phpArray as $key => $value) { 
     echo "<p>$key | $value</p>"; 
    } 
    ?> 

所有返回是

Torrent| 

如何繞過激流: 「」。

+0

什麼是'rss_php.php'? – JavaCake

+0

'$ some_var = json_decode(json_encode($ some_other_var));'?嗯... ... – jeroen

+0

'$ rss-> getRSS()'返回什麼? – jeroen

回答

0

我認爲rss_php是這件事: http://rssphp.net/download/

試試這個:

<?php 
require_once 'rss_php.php'; 
$rss = new rss_php; 
$rss->load('http://rss.thepiratebay.se/205'); 
print_r($rss->getRSS()); 
?> 

檢查網頁的源代碼。這是你陣列中的數據。你可以直接使用它,而無需使用json。因爲數組中有數組,所以不能輕鬆地對結果進行foreach。這樣做:

$arr = $rss->getRSS(); 
echo $arr['rss']['channel']['title']; 

編輯: 如果你想通過所有的結果的foreach,我建議是這樣的:

<?php 
require_once 'rss_php.php'; 
$rss = new rss_php; 
$rss->load('http://rss.thepiratebay.se/205'); 
$arr = $rss->getRSS(); 
//foreach over all the stuff in the channel 
foreach ($arr['rss']['channel'] as $key=>$val) 
{ 
    //In the array are keys like "title" and "comments", but we only want to iterate over the "item:1" (or some other number than 1), so only echo if the first 4 letters of the key are "item" 
    if (substr($key,0,4) == "item") 
    { 
    //echo the title, but you can also echo other things in that array. Check the code with the print_r to easily see what's in the feed 
    echo $val['title'].'<br />'; 
    } 
} 
?> 
+0

你是對的,非常感謝你的時間和耐心,這將幫助我很多。再次感謝 – user3199862