Accessing Array Values in the $_GET Superglobal
In PHP, the $_GET array contains GET parameters passed through the URL. It allows you to retrieve data from the query string of a web page. However, by default, $_GET values are scalars, meaning they can only hold a single value at a time.
Transforming GET Parameters into an Array
If you wish to pass multiple values for a GET parameter, you can use the following technique:
http://link/foo.php?id[]=1&id[]=2&id[]=3
Notice the syntax used for the id parameter: "id[]". By appending "[]" to the parameter name, you indicate that it should be treated as an array.
Accessing the Array
When you use the square bracket notation on the PHP side, you can access the array values:
<code class="php">echo $_GET['id'][0]; // Outputs "1" echo $_GET['id'][1]; // Outputs "2" echo $_GET['id'][2]; // Outputs "3"</code>
Alternative Solutions
If you're not able to use the "[]" syntax in the URL, there are other alternatives:
The above is the detailed content of How to Access Array Values in the $_GET Superglobal in PHP?. For more information, please follow other related articles on the PHP Chinese website!