From 2108af8f36611ac2ca97fbb95c8325ff78399b0e Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Mon, 22 Feb 2021 14:49:23 +0200 Subject: [PATCH] Added `Utils::arrayToQueryParams()` to convert an array into query params --- CHANGELOG.md | 2 ++ system/src/Grav/Common/Utils.php | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 020940583..e5abbbd0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # v1.7.7 ## mm/dd/2021 +1. [](#new) + * Added `Utils::arrayToQueryParams()` to convert an array into query params 1. [](#improved) * Added original image support for all flex objects and media fields 1. [](#bugfix) diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index cfb96bc33..a076f0aaa 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -1086,6 +1086,29 @@ abstract class Utils return $result; } + /** + * Flatten a multi-dimensional associative array into query params. + * + * @param array $array + * @param string $prepend + * @return array + */ + public static function arrayToQueryParams($array, $prepend = '') + { + $results = []; + foreach ($array as $key => $value) { + $name = $prepend ? $prepend . '[' . $key . ']' : $key; + + if (is_array($value)) { + $results = array_merge($results, static::arrayToQueryParams($value, $name)); + } else { + $results[$name] = $value; + } + } + + return $results; + } + /** * Flatten an array *