"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > 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?

Published on 2024-11-07
Browse:793

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:

print_r($_GET['id']); // Output: [1, 2, 3]

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

Release Statement This article is reprinted at: 1729586235 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3