user1897126
user1897126

Reputation: 9

php simple xml, loop hrough object, put string in array

I am getting an XML error for some reason, even though this(beginner) code does what i want it to do. It fetches strings into an array.

Line 11 results in "Notice: Trying to get property of non-object in D:\pam\w\www\mp\p\lasxml.php on line 11"

    <?php           
    $xml = new DOMDocument( "1.0", "ISO-8859-1" );                                                          
    $xml = simplexml_load_file('http://www.myepisodes.com/rss.php?feed=mylist&showignored=0&sort=asc&uid=Demerzel&pwdmd5=c6ed54b98a82b1----ac58147cedbde5'); 


    $allaFullStringfranxml = array();                                           
    $i = 0;                                                                 
        do{                                                                 
            $allaFullStringfranxml[] = $xml->channel->item[$i]->title;          
            ++$i;                                                               
        }while(($xml->channel->item[$i]->title) != null);                   

Upvotes: 1

Views: 118

Answers (1)

lupatus
lupatus

Reputation: 4248

I'm not sure why you are using do-while loop, I think in this case foreach will be better - do while loop is running at least once (even if there is no item). Also you are incrementing $i variable and then checking if title title of item with $i index is null - you shouldn't do that without checking if that item really exists. That should work better for you:

foreach ($xml->channel->item as $item) {
    $allaFullStringfranxml[] = (string)$item->title;
}

You can also notice here that I'm doing (string)$item->title - that will convert title node to string, in other case you'll store node object.

Upvotes: 1

Related Questions