1

I have a list of checkboxes in a GET form which results in a URL with a querystring like ?q=&cat=&city%5B%5D=28&city%5B%5D=26&sort=asc&order=name when submitted. The Object/Array behind this string looks like:

Array
(
    [q] => 
    [cat] => 
    [city] => Array
        (
            [0] => 28
            [1] => 26
        )
    [sort] => asc
    [order] => name
)

I'm trying to create a link with one of those GET params changed, but I can't find a way to update a single value. So now I'm rebuilding the URL by creating an object based on the given query params.

If I pass this object (as shown above) to the second argument inside Craft's url() function to use this URL on a simple <a> tag, I get an Array to string conversion error.

I seems Craft can't handle nested arrays in this function. Is there another method to generate an URL from such an object?

1

Yeah, looks like Craft's url() method doesn't support nested array syntax for querystring params.

If you still want to use it, then you'd need to flatten it with unique keys such as:

Array
(
    [q] => 
    [cat] => 
    [city0] => 28
    [city1] => 26
    [sort] => asc
    [order] => name
)

Alternatively you can manually build the URL yourself in Twig in whatever format you'd like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.