From 9eb97459679cd248804933cdf6d50d48ac19e7ed Mon Sep 17 00:00:00 2001 From: brettp Date: Wed, 7 Jul 2010 16:30:35 +0000 Subject: Added elgg_sort_3d_array_by_value(). I do this often enough that a helper function will be nice. git-svn-id: http://code.elgg.org/elgg/trunk@6657 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/elgglib.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'engine') diff --git a/engine/lib/elgglib.php b/engine/lib/elgglib.php index 385660d01..f41c87a7d 100644 --- a/engine/lib/elgglib.php +++ b/engine/lib/elgglib.php @@ -2410,6 +2410,37 @@ function elgg_get_array_value($key, array $array, $default = NULL) { return (isset($array[$key])) ? $array[$key] : $default; } +/** + * Sorts a 3d array by specific element. + * Will re-index numeric indexes. + * NB: This operates the same as the built-in sort functions. + * ie, sorts the array and returns a bool for success. + * + * Do this: elgg_sort_3d_array_by_value($my_array); + * Not this: $my_array = elgg_sort_3d_array_by_value($my_array); + * + * @param array $array Array to sort + * @param string $element Element to sort by + * @param $sort_order + * @param $sort_type + * @return bool + */ +function elgg_sort_3d_array_by_value(&$array, $element, $sort_order = SORT_ASC, $sort_type = SORT_LOCALE_STRING) { + $sort = array(); + + foreach ($array as $k => $v) { + if (isset($v[$element])) { + $sort[] = strtolower($v[$element]); + } else { + $sort[] = NULL; + } + } + + var_dump($sort, $array); + + return array_multisort($sort, $sort_order, $sort_type, $array); +} + /** * Some useful constant definitions */ -- cgit v1.2.3