开发者

php multidimensional array iteration issue

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){
        开发者_StackOverflow社区//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.


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;
}


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]);  
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜