Created
April 26, 2013 02:56
-
-
Save Narven/5464799 to your computer and use it in GitHub Desktop.
Sort an array based on a given field key http://www.zimplicit.se/knowledge/sort-multidimensional-array-based-given-key-value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Sort an array based on a given field key | |
* | |
* @param unknown_type $named_recs | |
* @param unknown_type $order_by | |
* @param unknown_type $rev | |
* @param unknown_type $flags | |
* @return unknown | |
*/ | |
function _named_records_sort($named_recs, $order_by, $reverse=false, $flags=0) { | |
// Create 1-dimensional named array with just sortfield (in stead of record) values | |
$named_hash = array(); | |
foreach($named_recs as $key=>$fields) | |
$named_hash["$key"] = $fields[$order_by]; | |
// Order 1-dimensional array, | |
// maintaining key-value relations | |
if($reverse) arsort($named_hash,$flags=0) ; | |
else asort($named_hash, $flags=0); | |
// Create copy of named records array | |
// in order of sortarray | |
$sorted_records = array(); | |
foreach($named_hash as $key=>$val) | |
$sorted_records["$key"]= $named_recs[$key]; | |
return $sorted_records; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment