Passing arrays as url parameter

Passing arrays as url parameter

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

Stefan Bogdanescu

Founder & Senior Architect

Passing Arrays as URL Parameters

You cannot simply concatenate a PHP array into a string to send it as a query parameter. The approach $url = 'http://www.example.com?aParam='.$aValues; will result in the literal text Array being sent to the server, or potentially cause a parse error depending on the environment. Similarly, appending the raw variable to a bracketed key like ?aParam[]= fails because the URL parser expects a value immediately following the equals sign, not a complex data structure.

To handle multiple values for a single parameter name, you must use the query string syntax that explicitly names each entry. The standard pattern is to append the key and value pairs sequentially, using the same key name for every item in the collection:

$baseUrl = 'http://www.example.com';
$values = ['apple', 'banana', 'cherry'];

// Constructing the query string manually
$queryParts = [];
foreach ($values as $value) {
    $queryParts[] = "aParam[]" = " . urlencode($value);
}
$url = $baseUrl . '?' . implode('&', $queryParts);

This results in a URL like http://www.example.com?aParam[]=apple&aParam[]=banana&aParam[]=cherry. While this is the fundamental mechanism, it quickly becomes unmanageable as your dataset grows. Manually looping through arrays to build strings is error-prone and violates the separation of concerns between data logic and string manipulation.

In modern PHP applications, you should rely on helper functions designed for this exact purpose. The http_build_query function is the standard utility that handles array serialization automatically. It takes an associative or indexed array and returns a properly encoded query string where necessary.

$values = ['apple', 'banana', 'cherry'];
$queryString = http_build_query($values, '', '[]', PHP_QUERY_RFC1738);
$url = 'http://www.example.com?' . $queryString;

Using http_build_query ensures that special characters are URL-encoded correctly and prevents double-encoding issues. It also respects the RFC 1738 standard for encoding, which is crucial for maintaining compatibility across different web servers and clients. Without this function, you risk breaking your application if a single value contains an ampersand or equals sign, which would otherwise terminate the parameter prematurely.

On the server side, how does the framework interpret these repeated keys? Most web frameworks, including Laravel, parse query strings into associative arrays by default. If your request is ?aParam[]=apple&aParam[]=banana, the framework will typically merge these into a single array entry rather than creating duplicate keys in the superglobal. However, behavior can vary based on configuration.

If you are using Laravel specifically, the framework abstracts this complexity away entirely. You do not need to manually construct the URL string at all. When building redirects or deep links, use the route helper or the Url facade, which handles parameter serialization automatically:

$url = url('example', ['param' => ['apple', 'banana', 'cherry']], true);

The third argument (true) ensures a relative path is returned, though absolute paths are equally valid. This approach eliminates the risk of encoding errors and keeps your code clean. It also leverages Laravel's internal routing logic to ensure the generated URL matches your defined routes exactly.

There is a trade-off to consider regarding how many parameters you pass. While passing an array as repeated keys works, some older systems or specific API endpoints might struggle with long query strings containing dozens of repeated parameters. In such cases, it is often better to serialize the array into a JSON string for transmission:

$payload = json_encode(['items' => $values]);
$url = 'http://www.example.com?data=' . urlencode($payload);

This shifts the data structure from a flat list of repeated keys to a single object. When receiving this on the server, you simply decode the JSON back into an array. This method is more scalable and avoids hitting query string length limits associated with repeating a key hundreds of times.

Ultimately, the choice depends on your API contract. If the receiving endpoint expects a repeated key structure, use http_build_query. If it accepts JSON payloads for complex data, serialization is the safer path. Always remember to verify how the target server parses incoming requests, as some legacy systems may interpret repeated query parameters differently than modern frameworks do.

Our network