php多维数组迭代问题

发布于 2024-12-09 03:43:02 字数 1483 浏览 0 评论 0原文

考虑有 4 个不同的 RSS 新闻提要 URL

$urls[0]['news'] = "url1";
$urls[1]['news'] = "url2";
$urls[2]['news'] = "url3";
$urls[3]['news'] = "url4";

以下函数应从每个 URL 获取 4 个新闻标题

function getRssFeeds($urls,$type){
        //Prepare XML objects
        $xmls[$type] = array();
        //Prepare item objects
        $items[$type] = array();
        //Prepare news titles/links arrays
        $article_titles[$type] = array();
        $encoded_titles[$type] = array();
        $article_links[$type] = array();
            //Fill XML objects
        for($i=0; $i<4; $i++){
            $xmls[$i][$type] = simplexml_load_file($urls[$i][$type]);
            //Prepare news items
            $items[$i][$type] = $xmls[$i][$type]->channel->item;
            for($i=0; $i<4; $i++){
                $article_titles[$i][$type] = $items[$i][$type]->title;
                $encoded_titles[$i][$type] = iconv("UTF-8","windows-1251",$article_titles[$i][$type]);
            }
            //$article_links[$type][$i] = $items[$type][$i]->link;
        }
        return $encoded_titles;
    }

使用以下命令获取值后:

 type='';
    function printRssFeed($urls,$type){
            $titles = getRssFeeds($urls,$type);
            foreach($titles as $title)
            {
                echo $title[$type]."<hr/>";
            }
        }

我收到未定义的偏移错误。如果我删除 getRssFeeds() 函数的内部 for 循环,我只能从每个 URL 获得 1 个新标题。

Consider having 4 different RSS news feeds URL's

$urls[0]['news'] = "url1";
$urls[1]['news'] = "url2";
$urls[2]['news'] = "url3";
$urls[3]['news'] = "url4";

The following function should get 4 news titles from each of the url's

function getRssFeeds($urls,$type){
        //Prepare XML objects
        $xmls[$type] = array();
        //Prepare item objects
        $items[$type] = array();
        //Prepare news titles/links arrays
        $article_titles[$type] = array();
        $encoded_titles[$type] = array();
        $article_links[$type] = array();
            //Fill XML objects
        for($i=0; $i<4; $i++){
            $xmls[$i][$type] = simplexml_load_file($urls[$i][$type]);
            //Prepare news items
            $items[$i][$type] = $xmls[$i][$type]->channel->item;
            for($i=0; $i<4; $i++){
                $article_titles[$i][$type] = $items[$i][$type]->title;
                $encoded_titles[$i][$type] = iconv("UTF-8","windows-1251",$article_titles[$i][$type]);
            }
            //$article_links[$type][$i] = $items[$type][$i]->link;
        }
        return $encoded_titles;
    }

After using the following to get the values:

 type='';
    function printRssFeed($urls,$type){
            $titles = getRssFeeds($urls,$type);
            foreach($titles as $title)
            {
                echo $title[$type]."<hr/>";
            }
        }

I get undefined offset error. If I remove the inner for loop of the getRssFeeds() function I get only 1 new title from each URL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯淡〆 2024-12-16 03:43:02

在此代码中,您将在内部 for 循环中将 $i 重置为 0。

 for($i=0; $i<4; $i++){
        $xmls[$i][$type] = simplexml_load_file($urls[$i][$type]);
        //Prepare news items
        $items[$i][$type] = $xmls[$i][$type]->channel->item;
        for($i=0; $i<4; $i++){
            $article_titles[$i][$type] = $items[$i][$type]->title;
            $encoded_titles[$i][$type] = iconv("UTF-8","windows-1251",$article_titles[$i][$type]);
        }
        //$article_links[$type][$i] = $items[$type][$i]->link;
    }

尝试将内部 for 循环变量更改为其他变量。此外,当您定义数组时,您似乎没有遵循相同的结构。

$xmls[$i][$type] 不=您的原始实例化 $xmls[$type] = array();
这对于所有其他数组都是如此。

所以我认为您的数组结构已关闭,因为您添加了 $type 的顶级,然后当您迭代时,您使用 $i 作为顶级键。

尝试删除开头的数组实例化

function getRssFeeds($urls,$type){
    //Fill XML objects
    for($i=0; $i<4; $i++){
        $xmls[$i][$type] = simplexml_load_file($urls[$i][$type]);
        //Prepare news items
        $items[$i][$type] = $xmls[$i][$type]->channel->item;
        for($i=0; $i<4; $i++){
            $article_titles[$i][$type] = $items[$i][$type]->title;
            $encoded_titles[$i][$type] = iconv("UTF-8","windows-1251",$article_titles[$i][$type]);
        }
        //$article_links[$type][$i] = $items[$type][$i]->link;
    }
    return $encoded_titles;
}

in this code you are resetting $i to 0 in your inner for loop.

 for($i=0; $i<4; $i++){
        $xmls[$i][$type] = simplexml_load_file($urls[$i][$type]);
        //Prepare news items
        $items[$i][$type] = $xmls[$i][$type]->channel->item;
        for($i=0; $i<4; $i++){
            $article_titles[$i][$type] = $items[$i][$type]->title;
            $encoded_titles[$i][$type] = iconv("UTF-8","windows-1251",$article_titles[$i][$type]);
        }
        //$article_links[$type][$i] = $items[$type][$i]->link;
    }

try changing your inner for loop variable to a different one. Also when you define your arrays it seems that you are not following the same structure.

$xmls[$i][$type] does not = your original instantiation of $xmls[$type] = array();
this is true for all your other arrays.

so I think your array structure is off because you add a top level of $type and then when you iterate you use a $i as you top level key.

try to remove the instantiations of the arrays in the beginning

function getRssFeeds($urls,$type){
    //Fill XML objects
    for($i=0; $i<4; $i++){
        $xmls[$i][$type] = simplexml_load_file($urls[$i][$type]);
        //Prepare news items
        $items[$i][$type] = $xmls[$i][$type]->channel->item;
        for($i=0; $i<4; $i++){
            $article_titles[$i][$type] = $items[$i][$type]->title;
            $encoded_titles[$i][$type] = iconv("UTF-8","windows-1251",$article_titles[$i][$type]);
        }
        //$article_links[$type][$i] = $items[$type][$i]->link;
    }
    return $encoded_titles;
}
要走干脆点 2024-12-16 03:43:02

在你的内部 for 循环中尝试这个

$j = 0;  
  for($j=0; $j<4; $j++){  
                $article_titles[$j][$type] = $items[$i][$type]->title;  
                $encoded_titles[$j][$type] = iconv("UTF-8","windows-1251",$article_titles[$j][$type]);  
            }

try this in your inner for loop

$j = 0;  
  for($j=0; $j<4; $j++){  
                $article_titles[$j][$type] = $items[$i][$type]->title;  
                $encoded_titles[$j][$type] = iconv("UTF-8","windows-1251",$article_titles[$j][$type]);  
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文