Created
January 23, 2011 11:26
-
-
Save tomschlick/792004 to your computer and use it in GitHub Desktop.
This function allows you to sort a php array by subelement values, this should be used in conjunction with the array_element function.
This file contains hidden or 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 | |
| function array_sort($array, $key, $order = 'asc', $sort_flags = SORT_REGULAR) | |
| { | |
| if( ! is_array($array)) | |
| { | |
| return FALSE; | |
| } | |
| foreach($array as $k=>$v) | |
| { | |
| $b[$k] = array_element($v, $key); | |
| } | |
| switch($order) | |
| { | |
| case 'asc': | |
| asort($b, $sort_flags); | |
| break; | |
| case 'desc': | |
| arsort($b, $sort_flags); | |
| break; | |
| } | |
| foreach($b as $key=>$val) | |
| { | |
| $c[$key] = $array[$key]; | |
| } | |
| return $c; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment