Get() does not support arrays

This function is really helpful. However it cannot handle arrays.

http://getkirby.com/docs/cheatsheet/helpers/get

When using $_POST for example you can have a little bit more complex forms with arrays. It would be helpful if this function could support arrays as well.

How do you submit you array?

The only way I know to submit an array of data to the server is to encode it as json, assign it to a POST parameter and decode the json on the server. I use this in a few web applications a have written.

You add like an array name.

Example

<form method="post">
  <input type="text" value="test" name="item[key]">
  <input type="submit" name="submitter">
</form>

Result

Array
(
    [item] => Array
        (
            [key] => Array
                (
                    [value] => test
                )

        )

    [submitter] => Send
)

I don’t knew that this is possible and it seems to me that you must be really careful about the things your code does with this data.
Anyway, if you like to change the code yourself, take a look at this file of the toolkit

Or please open an issue in the github toolkit repository, because Bastian can track issues better at github.

I just took a look at this and tested it with an example.

Query string

?test=test&test1[a]=a&test1[b]=b&test2[]=123&test2[]=456

PHP code

print_r(get());
print_r(get('test'));
print_r(get('test1'));
print_r(get('test2'));

Output

Array
(
    [test] => test
    [test1] => Array
        (
            [a] => a
            [b] => b
        )

    [test2] => Array
        (
            [0] => 123
            [1] => 456
        )

)

test

Array
(
    [a] => a
    [b] => b
)

Array
(
    [0] => 123
    [1] => 456
)

I think that works as intended. Am I missing something, @jenstornell?


For reference: Related GitHub issue: https://github.com/getkirby/toolkit/issues/149

Only that I posted it a year ago and that it’s fixed after that? :blush:

Probably solved then. Can’t test it on my vacation tho.

Have you tried it with method post in a form?

Well, the way I found this was the GitHub issue. It’s still open, so I don’t think this has been fixed. Would be great if you could take a look at it after coming back. Have a nice vacation!

When adding the issue I was using a form with method post with multiple array values but I can test it again in about 7 days.

Update

It works now.