Page 1 of 1
Rss feed read- array sorting by date
Posted: Tue Jul 31, 2012 7:12 am
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
Re: Rss feed read- array sorting by date
Posted: Tue Jul 31, 2012 11:03 pm
by Christopher
Re: Rss feed read- array sorting by date
Posted: Wed Aug 22, 2012 9:26 am
by vidhyakrish
hi
I have tried Usort().
Anybody has got any code help as im a PHP Newbie.
Thanks
Re: Rss feed read- array sorting by date
Posted: Wed Aug 22, 2012 9:38 am
by Christopher
Post the code you tried and we can help get it working.
Re: Rss feed read- array sorting by date
Posted: Wed Aug 22, 2012 9:59 am
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);
?>
Re: Rss feed read- array sorting by date
Posted: Wed Aug 22, 2012 1:22 pm
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.
Re: Rss feed read- array sorting by date
Posted: Thu Aug 23, 2012 5:17 am
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>';
?>
Re: Rss feed read- array sorting by date
Posted: Thu Aug 23, 2012 10:16 am
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.
Re: Rss feed read- array sorting by date
Posted: Thu Aug 23, 2012 10:31 am
by vidhyakrish
Hi christopher,
thanks for the guidance- much appreciated,
I just changed "strtolower" bits to "strtotime" and it did the magic!
Re: Rss feed read- array sorting by date
Posted: Thu Aug 23, 2012 6:42 pm
by Christopher
You won't be a "PHP Newbie" for long!