From: Tim Otten Date: Sat, 4 Jul 2015 01:15:44 +0000 (-0700) Subject: CRM_Utils_Array::rekey() X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=952d7eb120ffcb7a722fd136ab103e270bbddd60;p=civicrm-core.git CRM_Utils_Array::rekey() --- diff --git a/CRM/Utils/Array.php b/CRM/Utils/Array.php index 66af443f11..a2d16088d8 100644 --- a/CRM/Utils/Array.php +++ b/CRM/Utils/Array.php @@ -887,4 +887,21 @@ class CRM_Utils_Array { return $newRows; } + /** + * Rewrite the keys in an array by filtering through a function. + * + * @param array $array + * @param callable $func + * Function($key, $value). Returns the new key. + * @return array + */ + public static function rekey($array, $func) { + $result = array(); + foreach ($array as $key => $value) { + $newKey = $func($key, $value); + $result[$newKey] = $value; + } + return $result; + } + }