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?

PAndrews
  • 15
  • 3

2 Answers2

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',
  ],
]
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
  • This worked. clearly something is overriding how the variables are being parsed that I need to find and adjust. Thank you – PAndrews Aug 28 '17 at 17:46
0

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

Documentation

t1gor
  • 1,244
  • 12
  • 25
  • 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 – PAndrews Aug 28 '17 at 16:17
  • 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 – t1gor Aug 28 '17 at 16:23