Link to home
Start Free TrialLog in
Avatar of detox1978
detox1978Flag for United Kingdom of Great Britain and Northern Ireland

asked on

PHP: Sort array by date

Hi All,

I have a PHP array called $template['rows'] that has a key called date, samples below, does anyone know how i can sort the array by date?



3 June 2010, 12:52 pm
16 June 2010, 8:07 pm
15 June 2010, 7:27 pm
15 June 2010, 2:47 pm
14 June 2010, 10:49 pm
11 June 2010, 6:43 pm
10 June 2010, 9:02 pm
8 June 2010, 4:58 pm
8 June 2010, 4:54 pm
7 June 2010, 10:33 am
3 June 2010, 12:52 pm
15 June 2010, 7:06 pm
14 June 2010, 6:58 pm
12 June 2010, 9:30 am
11 June 2010, 10:11 am
10 June 2010, 9:17 pm
10 June 2010, 8:41 am
4 June 2010, 7:40 pm
4 June 2010, 7:06 pm
26 May 2010, 9:07 am
21 May 2010, 9:48 am
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Keep all internal representations of the date in ISO8601 format.  Then they will sort easily!
Avatar of detox1978

ASKER

I can change the format.


Currently its

$date_format = '%a, %e %b %Y, %I:%M %p';
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm struggling to get my head around your samples.

This is my PHP code i use to echo the array;

I need to sort the array by $row['date']
<?	
	foreach($template['rows'] as $row){
//			echo "Link:"		. $row['permalink'] . "<br />";
//			echo "Title:"		. $row['title'] . "<br />";
//			echo "Desc:"		. $row['desc'] . "<br />";
//			echo "New Image:"	. $row['new'] . "<br />"; 
			echo "Date:"		. $row['date'] . "<br />";

}

?>

Open in new window

That's quite a bit different from what you posted originally, where you said that you had a KEY with the date.  A key in an array is a term of art, but this new example shows that you have an element in the array with a key of 'date' and a value that needs sorting.

Here is what I would do.  Keep the dates in the $row['date'] field in ISO8601 format.  Then you can SELECT the rows out of your data base and ORDER BY 'date' - easy!

You can learn more about PHP and SQL date/time processing in my article here at EE.
https://www.experts-exchange.com/articles/Web_Development/Web_Languages-Standards/PHP/Handling-date-and-time-in-PHP-and-MySQL.html

HTH, ~Ray
Sidebar note (you will learn this when you read the article) about ISO8601...

In PHP this is date('c')

It looks a lot like YYYY-MM-DD HH:MM:SS
The data is from an RSS feed so i can't order by date.
Sorry for not being clear in my question, it's 1am here;


I've just noticed the data is not in one array, its a function that is called several times.  So would need to push the data into one array and then sort it.


the foreach loop can be used to push the data into one array.

<?
      foreach($template['rows'] as $row){
//                  echo "Link:"            . $row['permalink'] . "<br />";
//                  echo "Title:"            . $row['title'] . "<br />";
//                  echo "Desc:"            . $row['desc'] . "<br />";
//                  echo "New Image:"      . $row['new'] . "<br />";
                  echo "Date:"            . $row['date'] . "<br />";
}
?>




any help is welcome....
When you say it's from "a function that is called several times;" can you provide the section of code where this takes place?  That way, we'll know what variables to use when we (theoretically) put the all together into one array.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Function is below.


include('templates/feeds-list.php');  is the foreach loop above that creates the $html


It is called a few times, and i need to add all the calls into an array and on the page that calls it, resort by date.
	function listing($url, $options = null){
		if (!$options) $options = array();
		extract(newsblocks::data($url, $options));
		if (!$classname) $classname = 'feed';
		$template = array(
			'classname'=>$classname,
			'favicon'=>$favicon,
			'permalink'=>$permalink,
			'title'=>$title,
			'rows'=>array()
		);
		$counter_start = 0;
		$counter_length = $items;
		foreach ($feed->get_items($counter_start, $counter_length) as $item){
			$class = '';
			$type = '';
			$new = '';
			extract(newsblocks::has_enclosure($item));
			if ($item->get_date('U') > $since){
				$new = NEW_HTML;
			}
			$desc = "<b>" . $item->get_date() . "</b><br />" . substr(strip_tags($item->get_description(true)), 0, 500) . "...";
			$title_attr = newsblocks::get_title_attr($item, $length, $date_format);
			$template['rows'][] = array(
				'new'=>$new,
				'date'=>$item->get_date(),
				'desc'=>$desc,
				'permalink'=>$item->get_permalink(),
				'title_attr'=>$title_attr,
				'title'=>$item->get_title()
			);
		}
		ob_start();
		include('templates/feeds-list.php');
		$html = ob_get_contents();
		ob_end_clean();
//		unset($template);
		return $html;
	}

Open in new window

I've made it a lot simpler by passing a counter variable to each run of the for look so now i should be able to push the data into a new array.

I've created a new question for help on creating/managing the Array;


https://www.experts-exchange.com/questions/26265522/PHP-Create-an-Array.html



When i work that out i'll be able to test your scripts....  Thanks again for the help.


Thanks for the great help....
Glad you've got it moving forward... regarding this: "The data is from an RSS feed so i can't order by date." There is an easy solution - put the RSS feed into your data base!  Use SimpleXML to extract the important fields like the date and keep these in columns so you can select and order the data.  You can use temporary tables if you don't want to keep the feeds over long periods of time, like more than an instance of your script.  HTH, ~Ray
This seems like a much better way of doing it.


I've created a question here for assistance.
https://www.experts-exchange.com/questions/26267103/PHP-import-several-RSS-feeds-into-MySQL.html