protected static function encodeArrayVariable(array $variable, array $value, $operator, $separator, &$useQueryString)
    {
        $isAssociativeArray = self::isAssociative($variable);
        $keyValuePairs = [];

        foreach ($variable as $key => $var) {
            if ($isAssociativeArray) {
                $key = rawurlencode($key);
                $isNestedArray = is_array($var);
            } else {
                $isNestedArray = false;
            }

            if (!$isNestedArray) {
                $var = rawurlencode($var);
                if ($operator === '+' || $operator === '#') {
                    $var = self::decodeReservedDelimiters($var);
                }
            }

            if ($value['modifier'] === '*') {
                if ($isAssociativeArray) {
                    if ($isNestedArray) {
                        // allow for deeply nested structures
                        $var = strtr(http_build_query([$key => $var]), ['+' => '%20', '%7e' => '~']);
                    } else {
                        $var = $key . '=' . $var;
                    }
                } elseif ($key > 0 && $useQueryString) {
                    $var = $value['value'] . '=' . $var;
                }
            }

            $keyValuePairs[$key] = $var;
        }

        $expanded = '';
        if (empty($variable)) {
            $useQueryString = false;
        } elseif ($value['modifier'] === '*') {
            $expanded = implode($separator, $keyValuePairs);
            if ($isAssociativeArray) {
                // Don't prepend the value name when using the explode modifier with an associative array
                $useQueryString = false;
            }
        } else {
            if ($isAssociativeArray) {
                // The result must be a comma separated list of keys followed by their respective values
                // if the explode modifier is not set on an associative array
                foreach ($keyValuePairs as $k => &$v) {
                    $v = $k . ',' . $v;
                }
            }
            $expanded = implode(',', $keyValuePairs);
        }

        return $expanded;
    }