docs:programming:php:array_multisort

array_multisort

// $items is a keyed/associative array; each item has various key value
// combinations as well, one of which is a date timestamp
 
reset($items);
foreach($items as $key=>&$item){
	$sort[$key] = $item['datestamp'];
}
 
array_multisort($sort, SORT_DESC, $items);
 
// $items is now sorted by date

The trick is knowing the key for the value you are sorting with, and putting the parameters in the correct order to array_multisort. If you don't have a keyed array, you just need to figure out the index number (0, 1, 2, etc…), since all arrays are really keyed in some fashion anyway.

  • docs/programming/php/array_multisort.txt
  • Last modified: 2011/03/23 10:14
  • by billh