Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP RSS获取并显示多个RSS频道的标题_Php_Codeigniter_Rss - Fatal编程技术网

PHP RSS获取并显示多个RSS频道的标题

PHP RSS获取并显示多个RSS频道的标题,php,codeigniter,rss,Php,Codeigniter,Rss,我有以下代码: $rss[] = $this->rssparser->set_feed_url('someUrl')->set_cache_life(30)->getFeed(4); $rss[] = $this->rssparser->set_feed_url('someURL')->set_cache_life(30)->getFeed(3); $rss[] = $this->rsspars

我有以下代码:

        $rss[] = $this->rssparser->set_feed_url('someUrl')->set_cache_life(30)->getFeed(4);
        $rss[] = $this->rssparser->set_feed_url('someURL')->set_cache_life(30)->getFeed(3);
        $rss[] = $this->rssparser->set_feed_url('someURL')->set_cache_life(30)->getFeed(5);
它们中的每一个都显示在不同的div中,我想要的是相关频道的标题显示在相关div中

到目前为止,我已经:

foreach ($rss as $feed)
{
    $channel = $this->rssparser->channel_data['title'];

    $result = "<div class='rssFeed'>";
    $result .= '<h3>'.$channel.'</h3>';

    foreach ($feed as $item)
         {  
        $result .= '<div class="rssContent">
            <a href="'.$item['link'].'">'.$item['title'].'</a>
            <br />
            <span>'.$item['pubDate'].'</span>
            </div>
            ';
        }
            $result .= '</div>';

            echo $result;

        }
但这只会在顶部显示数组最后一个通道的标题


有人知道我做错了什么吗?

您可以在获取下一个提要之前存储频道标题。例如:-

<?php
class Controller
{
    public function action()
    {
        $feeds = array(
            array('url' => 'http://example.org/feed.rss', 'count' => 3),
            array('url' => 'http://example.org/feed.rss', 'count' => 4),
            array('url' => 'http://example.org/feed.rss', 'count' => 5),
        );

        $rss = array();

        foreach ($feeds as $feed) {
            $rss[] = array(
                'items' => $this->rssparser->set_feed_url($feed['url'])->set_cache_life(30)->getFeed($feed['count']),
                'title' => $this->rssparser->channel_data['title'],
            );
            $this->rssparser->clear();
        }
    }
}
然后在提要上进行如下迭代:-

<?php foreach ($rss as $feed): ?>
    <h1><?php echo $feed['title']; ?></h1>
    <?php foreach ($feed['items'] as $item): ?>
        <a href="<?php echo $item['link']; ?>">
            <?php echo $item['title']; ?>
        </a>
    <?php endforeach; ?>
<?php endforeach; ?>