In the process of PHP programming, we often need to take out data from the array for use, and it is more common to use the GET method to obtain the passed parameters. Here is a brief introduction on how to use the GET method to obtain the array.
The GET method is an HTTP request method used to obtain resources from the server. When we enter the URL address in the browser address bar, the browser uses the GET method to request resources from the server. In PHP, we can obtain the parameters passed by the GET method through the super global variable $_GET. When using the GET method to pass parameters to the server, you can splice the parameters at the end of the URL address in the form of name=value, and use the & symbol to connect multiple parameters. For example:
http://www.example.com/index.php?name=张三&age=20
In this URL address, name and age are both parameter names, and Zhang San and 20 are the corresponding parameter values respectively. In PHP, you can use the $_GET array to obtain these values, for example:
$name = $_GET['name']; $age = $_GET['age'];
In the above code, when we send a GET request, we use the $_GET array to obtain the name and age parameters. But how to get it when the passed parameter is an array? At this time we need to use some special methods to deal with it.
When using the GET method to pass an array, you can use the array elements as parameter values, use [] to represent the array subscript, and then connect multiple parameters with the & symbol. For example:
http://www.example.com/index.php?name[]=张三&name[]=李四&age=20
In this URL address, name[] is used to represent the array, and its array elements are Zhang San and Li Si. In PHP, you can use the $_GET array to get the element value in the name array. The code is as follows:
$name = $_GET['name']; //这里得到的是一个数组 $age = $_GET['age']; echo $name[0]; //输出“张三” echo $name[1]; //输出“李四”
In the above code, we get an array through $_GET['name'], and then we can use The subscript of the array to get the element value.
In addition to using name[] to represent array elements, you can also use name[key] to represent array elements. For example:
http://www.example.com/index.php?name[0]=张三&name[1]=李四&age=20
In this URL address, name[0] and name[1] are used to represent array elements respectively, and their values are Zhang San and Li Si respectively. In PHP, you can also use the $_GET array to obtain the value of the array element. The code is as follows:
$name = $_GET['name']; //这里得到的是一个数组 $age = $_GET['age']; echo $name[0]; //输出“张三” echo $name[1]; //输出“李四”
When using the GET method to obtain parameters, you need to pay attention to the following points:
1. Use GET When the method passes parameters, it should not contain sensitive information, because all parameters will appear in the URL address and can be easily intercepted and stolen.
2. When using the $_GET array to obtain parameters, you should first determine whether the parameters exist to avoid errors caused by non-existent parameters.
3. When using the GET method to pass an array, special operations are required to obtain the element values.
In short, through the above method, we can easily use the GET method to obtain the array element value in the parameter, so that it can be used more flexibly in PHP programming.
The above is the detailed content of PHP uses get method to get array. For more information, please follow other related articles on the PHP Chinese website!