How to pass arrays as a URL parameter with PHP?

Spread the love

Sometimes, we want to pass arrays as a URL parameter with PHP.

In this article, we’ll look at how to pass arrays as a URL parameter with PHP.

How to pass arrays as a URL parameter with PHP?

To pass arrays as a URL parameter with PHP, we can use the http_build_query function.

For instance, we write

$data = [1, 4, "a" => "b", "c" => "d"];
$query = http_build_query(["aParam" => $data]);

to call http_build_query with an associative array.

The keys will be returned as the query parameter keys and the values are the query parameter values.

The values are escaped automatically.

Conclusion

To pass arrays as a URL parameter with PHP, we can use the http_build_query function.

Leave a Reply

Your email address will not be published.