0

I know I should be able to create arrays through GET by creating a url such as

?sort[]=four&sort[price]=five&sort[status]=good

and receive

$_GET['sort'] = ['four','price'=>'five','status'=>'good'];

but instead, when trying to access $_GET['sort'] I get an undefined index for 'sort' and the only keys for $_GET are 'sort[]', 'sort[price]', 'sort[status]'.

What do I need to change to make this work as expected?

5
  • I would advice looking into this solution: stackoverflow.com/questions/1763508/… Commented Aug 28, 2017 at 15:56
  • why you use same get parameter ? Commented Aug 28, 2017 at 15:56
  • Possible duplicate of How to send PHP array by html form by POST type Commented Aug 28, 2017 at 15:59
  • print_r($_GET['sort']); is working fine for me. Commented Aug 28, 2017 at 16:01
  • @thefolenangel I actually attempted that solution prior to asking the question and the keys for GET in that case are 'sort%5B0%5D', so essentially the same issue Commented Aug 28, 2017 at 16:13

2 Answers 2

1

Your GET query string is correctly parsed by PHP on my machine so I don't think there is anything wrong with your syntax.

What you can do as a workaround is manually parse the query string and populate $_GET

parse_str($_SERVER['QUERY_STRING'], $_GET);

$_GET is now:

[
  'sort' => [
      0        => 'four',
      'price'  => 'five',
      'status' => 'good',
  ],
]
Sign up to request clarification or add additional context in comments.

1 Comment

This worked. clearly something is overriding how the variables are being parsed that I need to find and adjust. Thank you
0

"http://example.com?".http_build_query(['four','price'=>'five','status'=>'good'])

Documentation

2 Comments

this works for sending individual keys which I wasn't having problems with before, but this method still doesn't work for what I am trying to do, which is "http://example.com?".http_build_query(['sort'=>['four','price'=>'five','status'=>'good']]). gives me $_GET['sort%5B0%5D' => 'four',...] etc
See example 3 on docs page. If that still does not work with GET, you can try to parse the query string by yorself. See parse_str

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.