Return to Snippet

Revision: 20507
at November 17, 2009 11:33 by leonelsantos


Initial Code
function compare ($a, $b) {
	//return strcmp($a["fruit"], $b["fruit"]);
	// in the case of the above array
	// note we are comparing the first element of each array
	if ( $a[0] < $b[0] ) {
		return -1;
	}
	if ( $a[0] > $b[0] ) {
		return 1;
	}
	// they are equal
	return 0;
}

usort($events, 'compare');

Initial URL
www.coderemix.com

Initial Description
For more information, see php.net, under usort

It works with dates

Replace 0 for the array's key you want to sort. For example, if you have a start_date array key you would like to compare, then you would replace 0 for start_date.

If you want to do a reverse sort, just switch the -1 and 1.

Initial Title
PHP Compare Multi-dimensional Array Function

Initial Tags
array

Initial Language
PHP