]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Added elgg_sort_3d_array_by_value(). I do this often enough that a helper function...
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Wed, 7 Jul 2010 16:30:35 +0000 (16:30 +0000)
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Wed, 7 Jul 2010 16:30:35 +0000 (16:30 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@6657 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/lib/elgglib.php

index 385660d0103c8d921d9535f7a4cb94ea2b9ce571..f41c87a7da3d11199fd7c6ee932c29484628fb2d 100644 (file)
@@ -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
  */