diff options
Diffstat (limited to 'app/functions.php')
-rw-r--r-- | app/functions.php | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/app/functions.php b/app/functions.php index 8f0d482c..e732f308 100644 --- a/app/functions.php +++ b/app/functions.php @@ -53,6 +53,37 @@ function array_column_index(array &$input, $column) } /** + * Create indexed array from a list of dict with unique values + * + * $input = [ + * ['k1' => 1, 'k2' => 2], ['k1' => 3, 'k2' => 4], ['k1' => 1, 'k2' => 5] + * ] + * + * array_column_index_unique($input, 'k1') will returns: + * + * [ + * 1 => ['k1' => 1, 'k2' => 2], + * 3 => ['k1' => 3, 'k2' => 4], + * ] + * + * @param array $input + * @param string $column + * @return array + */ +function array_column_index_unique(array &$input, $column) +{ + $result = array(); + + foreach ($input as &$row) { + if (isset($row[$column]) && ! isset($result[$row[$column]])) { + $result[$row[$column]] = $row; + } + } + + return $result; +} + +/** * Sum all values from a single column in the input array * * $input = [ |