Handling Arrays in Query Strings: A Practical Guide to $_GET
When building web applications, passing data between pages is a fundamental operation. The standard approach involves appending key-value pairs to the URL, such as ?a=1&b=2&c=3. This format works perfectly for scalar values, but it breaks down when you need to transmit an array or a list of related items. A common misconception is that you can simply pass a serialized string like ?a=[1,2,3] and expect the server to automatically parse it into a native PHP array.
In reality, browsers do not natively understand JSON or other data serialization formats within query strings unless explicitly told to do so. If you send ?a=1&b=2, PHP's $_GET superglobal treats each parameter as a scalar string. To pass multiple values under the same key, PHP relies on a specific naming convention: appending brackets to the key name for each additional item.
If you submit a form with two checkboxes named colors containing "red" and "blue", the resulting URL will look like this:?colors=red&colors=blue
When PHP parses this request, it does not create an array of strings; instead, it creates an associative array where the key is "colors" and the values are indexed numerically. Accessing $_GET['colors'] returns Array ( [0] => red [1] => blue ). This behavior allows you to reconstruct arrays on the receiving end without needing a complex for-loop to append values manually, provided the input follows this bracket-based convention.
However, relying solely on repeated keys often leads to messy URLs and can be difficult to read or maintain. A more robust pattern, especially when dealing with structured data, is to use index notation in the URL itself. Instead of repeating the key name, you explicitly define the indices:?a[0]=1&a[1]=2&a[2]=3
When PHP processes this request, it automatically populates $_GET['a'] as a numerically indexed array containing [1, 2, 3]. This method is cleaner because it clearly delineates the structure of the data in the URL and avoids potential collisions with other keys named a.
There are distinct trade-offs to consider between these approaches. The repeated key method (colors=red&colors=blue) is legacy behavior that works well for simple forms but can be confusing when debugging URLs. The indexed method (a[0]=1&a[1]=2) is generally preferred for programmatic data transfer because it maintains order and structure explicitly. However, both methods have a limitation: they only support native PHP array types when the incoming data is strictly numerical or follows the specific key repetition rules defined by the HTTP specification.
If you attempt to pass JSON directly, such as ?data={"name":"John","age":30}, PHP will treat the entire value as a single string unless you configure your application to parse it. You cannot simply rely on $_GET to convert that string into an associative array automatically. To handle this scenario, you must manually decode the string using json_decode() after retrieving the raw input.
$data = $_GET['data'];
$decodedData = json_decode($data, true);
// $decodedData is now a standard PHP array
if (is_array($decodedData)) {
// Proceed with logic
}
This manual decoding adds a layer of responsibility to your code. You must validate that the input is valid JSON before attempting to decode it, or you risk runtime errors if the user sends malformed data. This distinction is crucial when deciding whether to stick to simple query parameter arrays or switch to POST requests with application/json content types for complex payloads.
For frameworks like Laravel, handling arrays via $_GET remains consistent with core PHP behavior, but the framework provides utilities that can abstract away some of these manual steps if you are working with form requests or API controllers. If you are maintaining a legacy codebase where simple query strings are the only option, using the indexed notation (key[index]) is usually the most reliable way to ensure your arrays are passed correctly without writing custom parsing logic.