Home > Backend Development > PHP Tutorial > 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
Release: 2024-10-22 20:38:02
Original
942 people have browsed it

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
Copy after login

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
Copy after login

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>
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template