Created
September 12, 2012 07:29
-
-
Save bohman/3704955 to your computer and use it in GitHub Desktop.
sort_array_on_value() - sorting multidimensional arrays into hyperspace
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
// | |
// Easy lil' sorting thingamajig. Mad creds to Antonio Navarro of 040. | |
// | |
function sort_array_on_value($array_to_sort, $sort_on_value, $descending = false) { | |
$sortArr = array(); | |
foreach ($array_to_sort as $key => $value) { | |
$sortArr[$key] = $value[$sort_on_value]; | |
} | |
if ($descending) { | |
arsort($sortArr); | |
} else { | |
asort($sortArr); | |
} | |
$resultArr = array(); | |
foreach ($sortArr as $key => $value) { | |
$resultArr[$key] = $array_to_sort[$key]; | |
} | |
return $resultArr; | |
} |
Way cool. Thanks for sharing Andreas!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This inspired me to update and share my Sorting-class. It might help you if you need something more advanced.
Check out https://gist.github.com/3705706