"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 Pass and Access Array Values in PHP $_GET?

How to Pass and Access Array Values in PHP $_GET?

Published on 2024-11-03
Browse:863

How to Pass and Access Array Values in PHP $_GET?

PHP $_GET Array as an Array

The $_GET array in PHP is a superglobal variable that can be used to obtain information sent via a URL query string. Typically, each key in the array represents a variable name, while the corresponding value contains the associated value. By default, the $_GET values are treated as strings.

Sending an Array Value in $_GET

However, it is possible to pass an array value in the $_GET query string. To do this, you need to use the following syntax:

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

In this case, the "id" parameter becomes an array with three elements, each containing one of the values provided in the query string.

Accessing the Array Value in PHP

On the PHP side, you can access the array value using the following syntax:

$_GET['id'];

This will return an array containing the three values that were passed in the query string.

Example

Consider the following PHP script:

If you access this script via a URL like the following:

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

The script will output the following array:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Release Statement This article is reprinted at: 1729585877 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