mraza Posted April 9, 2011 Share Posted April 9, 2011 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 More sharing options...
.josh Posted April 9, 2011 Share Posted April 9, 2011 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 More sharing options...
mattal999 Posted April 9, 2011 Share Posted April 9, 2011 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... Link to comment https://forums.phpfreaks.com/topic/233199-how-to-sort-a-feed/#findComment-1199298 Share on other sites More sharing options...
.josh Posted April 9, 2011 Share Posted April 9, 2011 @mattal999 : rsort() sorts descending alphabetically...not the same as outputting in reverse order. Link to comment https://forums.phpfreaks.com/topic/233199-how-to-sort-a-feed/#findComment-1199299 Share on other sites More sharing options...
mattal999 Posted April 9, 2011 Share Posted April 9, 2011 $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. Link to comment https://forums.phpfreaks.com/topic/233199-how-to-sort-a-feed/#findComment-1199300 Share on other sites More sharing options...
.josh Posted April 9, 2011 Share Posted April 9, 2011 $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 Link to comment https://forums.phpfreaks.com/topic/233199-how-to-sort-a-feed/#findComment-1199302 Share on other sites More sharing options...
mraza Posted April 9, 2011 Author Share Posted April 9, 2011 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 More sharing options...
mattal999 Posted April 9, 2011 Share Posted April 9, 2011 If you wanted more than one piece of information, then use a multi-dimensional array. Change: $array[] = $item->title; To: $array[] = array( "title" => $item->title, "link" => $item->link ); Link to comment https://forums.phpfreaks.com/topic/233199-how-to-sort-a-feed/#findComment-1199304 Share on other sites More sharing options...
mraza Posted April 9, 2011 Author Share Posted April 9, 2011 Ok thanks for help. all sorted. Link to comment https://forums.phpfreaks.com/topic/233199-how-to-sort-a-feed/#findComment-1199325 Share on other sites More sharing options...
mraza Posted April 13, 2011 Author Share Posted April 13, 2011 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 More sharing options...
mraza Posted April 13, 2011 Author Share Posted April 13, 2011 nvm got it sorted with "description" =>(string) $description, Regards Link to comment https://forums.phpfreaks.com/topic/233199-how-to-sort-a-feed/#findComment-1201046 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.