从json中提取php数据

时间:2014-01-15 20:23:14

标签: php json

我正试图从json中提取一些数据而且我被卡住了。这里是json的开头:

    {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|

如何绕过洪流:“”。

1 个答案:

答案 0 :(得分: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。因为数组中有数组,所以无法轻松地预测结果。做类似的事情:

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

编辑: 如果你想要了解所有结果,我建议这样的事情:

<?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 />';
  }
}
?>