Home  >  Article  >  Backend Development  >  How to Retrieve Multiple Values from a GET Parameter as an Array in PHP?

How to Retrieve Multiple Values from a GET Parameter as an Array in PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-22 20:38:02869browse

How to Retrieve Multiple Values from a GET Parameter as an Array in PHP?

Accessing Values as an Array in PHP $_GET

In PHP, the $_GET superglobal variable provides a way to access data sent from a web form or a URL query string. However, it's not immediately clear how to obtain values in the $_GET array as an array.

Let's consider a scenario where you want to send multiple values for the "id" parameter in a URL:

http://link/foo.php?id=1&id=2&id=3

If you attempt to access the "id" value using $_GET['id'], you'll only get the last value (in this case, "3"). To retrieve the values as an array, you can modify your URL to include square brackets ("[]") after the parameter name:

http://link/foo.php?id[]=1&id[]=2&id[]=3

Now, if you access $_GET['id'], you'll obtain an array containing all the "id" values:

<code class="php">print_r($_GET['id']); // Output: [1, 2, 3]</code>

This approach allows you to easily access multiple values for a single parameter in your PHP code.

The above is the detailed content of How to Retrieve Multiple Values from a GET Parameter as an Array in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn