How to pass an array through $_GET array?

How to pass an array through $_GET array?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Pass an Array Through $_GET

By default, PHP treats query parameters as a flat key-value map. When you send a URL like http://link/foo.php?id=1&id=2&id=3, the superglobal $_GET['id'] contains only the final value provided, which in this case is "3". This behavior stems from how standard URL parsing works: subsequent keys with identical names overwrite previous ones within the associative array structure. If your application logic requires handling multiple values for a single parameter simultaneously, you must explicitly instruct the web server to parse these duplicates into an array.

The Role of array_parse_params and useQueryStrings

To convert repeated query string parameters into an array in PHP, you need to enable specific parsing behaviors. Historically, this was controlled by Apache directives or specific PHP settings depending on the version. In modern PHP versions (5.4+), the behavior is governed by the array_parse_params setting within the .htaccess file or php.ini.

You can activate array parsing for all query parameters by adding the following directive to your Apache configuration:

<IfModule mod_php7>
    php_flag array_parse_params 1
</IfModule>

Once this flag is set, PHP will interpret repeated keys in the $_GET superglobal as an array. Accessing $_GET['id'] after enabling this setting will return ['1', '2', '3'] instead of just '3'. Without this directive enabled, PHP adheres to strict associative array rules where later entries overwrite earlier ones, which is why you initially saw only the last ID.

Handling Query Strings in Laravel

If your stack includes Laravel, managing repeated query parameters involves a different approach because the framework abstracts much of the raw $_GET handling to prevent security vulnerabilities like mass assignment and unpredictable array structures. While you can technically enable array_parse_params globally, it is often safer and cleaner to handle this explicitly within the application code or route definition.

Laravel provides the request helper which allows you to access query parameters as an array directly if they are repeated in the URL, provided the underlying PHP setting allows it. However, a more robust pattern in modern Laravel applications is to define your routes using named parameters with specific types or constraints to avoid relying on raw superglobal behavior.

Consider a scenario where you want to pass an ID array without modifying server configuration:

// In a route definition (Laravel 8+)
Route::get('/foo', function ($request) {
    // If 'id' appears multiple times in the query string and 
    // PHP is configured to parse arrays, this returns an array.
    $ids = $request->input('id'); 

    if (!is_array($ids)) {
        $ids = [$ids]; // Fallback if only one ID was passed or parsing wasn't enabled
    }

    return response()->json(['ids' => $ids]);
})->name('foo.list');

In this example, the code defensively checks whether input('id') returns an array. If the server configuration (array_parse_params) is not set to 1, or if only one ID was sent, $request->input('id') will return a scalar string. Wrapping it in an array ensures your logic doesn't break when switching between single-value and multi-value requests.

Security Implications of Array Parsing

Enabling array_parse_params is not without risk. While convenient for handling lists of IDs, tags, or filters, it opens the door to potential security issues if you are not careful about how you process the data. For instance, if you iterate over this array without validating each element, you might inadvertently expose internal logic or cause performance bottlenecks with large arrays sent via GET parameters.

GET requests are typically used for stateless information retrieval, and browsers often limit the size of query strings. Sending a massive array of IDs in a single URL can lead to memory exhaustion on the server side if not validated. Always validate the size and content of incoming $_GET arrays before processing them in your application logic.

Alternative Approach: POST Requests for Lists

If enabling array_parse_params feels like too much configuration overhead or introduces unwanted side effects, consider using HTTP POST requests with application/x-www-form-urlencoded or multipart/form-data for data that requires array handling. While GET is the standard for simple queries, complex data structures or large lists of identifiers often fit better in POST bodies where you can control the encoding format strictly (e.g., JSON arrays) rather than relying on URL query string parsing quirks.

However, if your requirement is specifically to keep the interface as a clean URL like ?id=1&id=2, enabling array_parse_params remains the standard solution for PHP and Laravel environments that support it.

Our network