From 5eaa4292c6db2c1ee1282c938c899dc5b88db65f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 20 Apr 2020 11:49:17 +0200 Subject: [statistics] New filter UI --- inc/arrayutil.inc.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'inc') diff --git a/inc/arrayutil.inc.php b/inc/arrayutil.inc.php index ec6e2a5f..490b5a4f 100644 --- a/inc/arrayutil.inc.php +++ b/inc/arrayutil.inc.php @@ -10,7 +10,7 @@ class ArrayUtil * @param string $key * @return array */ - public static function flattenByKey($list, $key) + public static function flattenByKey(array $list, string $key) { $ret = []; foreach ($list as $item) { @@ -21,4 +21,25 @@ class ArrayUtil return $ret; } + /** + * Pass an array of arrays you want to merge. The keys of the outer array will become + * the inner keys of the resulting array, and vice versa. + * @param array $arrays + * @return array + */ + public static function mergeByKey(array $arrays) + { + $empty = array_combine(array_keys($arrays), array_fill(0, count($arrays), false)); + $out = []; + foreach ($arrays as $subkey => $array) { + foreach ($array as $key => $item) { + if (!isset($out[$key])) { + $out[$key] = $empty; + } + $out[$key][$subkey] = $item; + } + } + return $out; + } + } \ No newline at end of file -- cgit v1.2.3-55-g7522