Jump to content

Recommended Posts

Hi, I am reading a feed with this code

 

foreach ($xml->channel->item as $item) { 
echo $item->title;
echo '<br>';
}

and outputs

title 1
title 2
title 3
.... and so on until 10

 

I wants in reverse order to output last rss feed first like:

title 10
title 9
title 8
.... and so on until 1

, how can i do it? i tried with rsort($xml->channel->item) butt getting this error:

Warning: rsort() expects parameter 1 to be array, object given in

Thanks for help

Link to comment
https://forums.phpfreaks.com/topic/233199-how-to-sort-a-feed/
Share on other sites

well..only going by the code you posted, one way to do it would be

 

foreach ($xml->channel->item as $item) { 
  echo $titles[] = $item->title;
}
$titles = array_reverse($titles);
foreach ($titles as $title) { 
  echo $title . "<br/>";
}

 

Though I have a sneaking suspicion there might be a built-in way to do it, depending on what you are using to get that xml object

Link to comment
https://forums.phpfreaks.com/topic/233199-how-to-sort-a-feed/#findComment-1199297
Share on other sites

You'd need to first put all of the data into an array, and then sort it using that function.

 

$array = array();

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

$array = array_reverse($array);

foreach($array as $item) {
    echo $item . "<br />";
}

Edit: Crayon always beats me to it... :P

Link to comment
https://forums.phpfreaks.com/topic/233199-how-to-sort-a-feed/#findComment-1199298
Share on other sites

$mattal999 : rsort() sorts descending alphabetically...not the same as outputting in reverse order.

 

I was just going with the function that was originally suggested in the main post. I've updated my post to use array_reverse.

 

fair enough.  I almost did the same thing :P

Link to comment
https://forums.phpfreaks.com/topic/233199-how-to-sort-a-feed/#findComment-1199302
Share on other sites

Thank you sir, i actually had that array idea but is not there any other way that it can sort $xml items without adding this step to loop through all feed. Is it possible i will no need to assign new array for every elements i need like  -link -title etc and i dont need to change $xml->channel->item as $item  ?

 

Thanks and Regards

 

Edit: i m using this to get feed $xml = simplexml_load_file($feedurl);

Link to comment
https://forums.phpfreaks.com/topic/233199-how-to-sort-a-feed/#findComment-1199303
Share on other sites

HI, I am having a wierd problem using abvoe method. here is my code

$items = array();
foreach ($xmlload->channel->item as $item) { 
$ns_content = $item->children('http://purl.org/rss/1.0/modules/content/');
$desc = $ns_content->encoded;
$description = $item->description;
// echo $description; // this looks nomal here on echo
$categories = array();
                foreach ($item->children() as $child) {
                    if ($child->getName() == 'category') {
                        $categories[] = (string) $child;
                    }
                }

    $items[] = array(
				"title" => $item->title,
				"link" => $item->link,
				"desc" => $desc,
				"description" => $description,
				"categories" => $categories
			);
}

$items = array_reverse($items);	
print_r($items);

when i do print_r i get this result

[0] => Array
        (
            [title] => SimpleXMLElement Object
                (
                    [0] => Title of post
                )

            [link] => SimpleXMLElement Object
                (
                    [0] => http://www.linktofeed.html
                )
            [desc] => SimpleXMLElement Object
                (
                )

            [description] => SimpleXMLElement Object
                (
                )

            [categories] => Array
                (
                )

        )

 

in [description] i am getting SimpleXMLElement Object as description not original one. I tried to output echo as in above code

// echo $description; // this looks nomal here on echo

 

and there it display correct description but not when added in $items description. any help?

Thanks

Link to comment
https://forums.phpfreaks.com/topic/233199-how-to-sort-a-feed/#findComment-1201037
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.