Rss feed read- array sorting by date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vidhyakrish
Forum Newbie
Posts: 10
Joined: Mon Feb 27, 2012 11:21 am

Rss feed read- array sorting by date

Post by vidhyakrish »

Hi

the following code is used to read events feed-

Code: Select all

<?php
		  
$rss = new SimpleXMLElement('http://www.southlanarkshire.gov.uk/sllc/site/scripts/rss.php?events', null, true);

$num_items = 45;
$items = array_slice($rss->xpath('channel/item'), 0, $num_items); 

print_r($items);

?>
I would like to sort the above array by 'StartDate' - I tried few sortbyKey methods - but doesnt seem to work.

Any help?

Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Rss feed read- array sorting by date

Post by Christopher »

(#10850)
vidhyakrish
Forum Newbie
Posts: 10
Joined: Mon Feb 27, 2012 11:21 am

Re: Rss feed read- array sorting by date

Post by vidhyakrish »

hi

I have tried Usort().

Anybody has got any code help as im a PHP Newbie.

Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Rss feed read- array sorting by date

Post by Christopher »

Post the code you tried and we can help get it working.
(#10850)
vidhyakrish
Forum Newbie
Posts: 10
Joined: Mon Feb 27, 2012 11:21 am

Re: Rss feed read- array sorting by date

Post by vidhyakrish »

Hi

Below is the code i am using:

Code: Select all

<?php
		  
$rss = new SimpleXMLElement('http://www.southlanarkshire.gov.uk/sllc/site/scripts/rss.php?events', null, true);

$num_items = 45;
$items = array_slice($rss->xpath('channel/item'), 0, $num_items); 


print_r($items);

 




function sortByOneKey(array $array, $key, $asc = true) {
    $result = array();
       
    $values = array();
    foreach ($array as $id => $value) {
        $values[$id] = isset($value[$key]) ? $value[$key] : '';
    }
       
    if ($asc) {
        asort($values);
    }
    else {
        arsort($values);
    }
       
    foreach ($values as $key => $value) {
        $result[$key] = $array[$key];
    }
       
    return $result;
}
$sortedByNameAsc = sortByOneKey($items,'startDate');

print_r($sortedByNameAsc,true);

?>
Last edited by Benjamin on Wed Aug 22, 2012 11:50 am, edited 1 time in total.
Reason: Added [syntax=php||htm||css||javascript||sql||etc] - Please use [syntax] tags when posting code in the forums! Thanks.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Rss feed read- array sorting by date

Post by Christopher »

I copied the below code, pretty much directly, from usort() manual page:

Code: Select all

$rss = new SimpleXMLElement('http://www.southlanarkshire.gov.uk/sllc/site/scripts/rss.php?events', null, true);

$num_items = 45;
$items = array_slice($rss->xpath('channel/item'), 0, $num_items);


function rss_sort_asc($a, $b)
{
    $a_title = strtolower($a->title);
    $b_title = strtolower($b->title);
	if ($a_title == $b_title) {
        return 0;
    }
    return ($a_title < $b_title) ? -1 : 1;
}
usort($items, 'rss_sort_asc');
echo '<pre>' . print_r($items, 1) . '</pre>';
It seems to work.
(#10850)
vidhyakrish
Forum Newbie
Posts: 10
Joined: Mon Feb 27, 2012 11:21 am

Re: Rss feed read- array sorting by date

Post by vidhyakrish »

Hi christopher,
Thanks but I am afraid it doesnt work- It doesnt sort by 'Startdate' as expected- when i use the above code it starts from an event with Dec 2012 then Aug 2012 and so on.

I would like the array to be sorted by 'Startdate': I guess it is something to do with the date comparison!

For your refererence:

this is the Code i used:

Code: Select all

<?php
 
 $rss = new SimpleXMLElement('http://www.southlanarkshire.gov.uk/sllc/site/scripts/rss.php?events', null, true);

$num_items = 45;
$items = array_slice($rss->xpath('channel/item'), 0, $num_items);


function rss_sort_asc($a, $b)
{
    $a_startDate = strtolower($a->startDate);
    $b_startDate = strtolower($b->startDate);
        if ($a_startDate == $b_startDate) {
        return 0;
    }
    return ($a_startDate< $b_startDate) ? -1 : 1;
}
usort($items, 'rss_sort_asc');
echo '<pre>' . print_r($items, 1) . '</pre>';



?>
Last edited by Benjamin on Thu Aug 23, 2012 6:47 am, edited 1 time in total.
Reason: Added [syntax=php||htm||css||javascript||sql||etc] - Please use [syntax] tags when posting code in the forums! Thanks.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Rss feed read- array sorting by date

Post by Christopher »

vidhyakrish wrote:Thanks but I am afraid it doesnt work- It doesnt sort by 'Startdate' as expected- when i use the above code it starts from an event with Dec 2012 then Aug 2012 and so on.

I would like the array to be sorted by 'Startdate': I guess it is something to do with the date comparison!
If you search for "php date comparison" you will find many examples. Use that instead of the string/number comparison in the example.
(#10850)
vidhyakrish
Forum Newbie
Posts: 10
Joined: Mon Feb 27, 2012 11:21 am

Re: Rss feed read- array sorting by date

Post by vidhyakrish »

Hi christopher,

thanks for the guidance- much appreciated,

I just changed "strtolower" bits to "strtotime" and it did the magic!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Rss feed read- array sorting by date

Post by Christopher »

You won't be a "PHP Newbie" for long!
(#10850)
Post Reply