php多维数组迭代问题
考虑有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在此代码中,您将在内部 for 循环中将 $i 重置为 0。
尝试将内部 for 循环变量更改为其他变量。此外,当您定义数组时,您似乎没有遵循相同的结构。
$xmls[$i][$type]
不=您的原始实例化$xmls[$type] = array();
这对于所有其他数组都是如此。
所以我认为您的数组结构已关闭,因为您添加了
$type
的顶级,然后当您迭代时,您使用$i
作为顶级键。尝试删除开头的数组实例化
in this code you are resetting $i to 0 in your inner for loop.
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
在你的内部 for 循环中尝试这个
try this in your inner for loop