In PHP, you can use multiple methods to pass arrays. This article will introduce three commonly used methods of passing arrays in PHP: GET, POST and SESSION.
The GET method is one of the simplest ways to pass data. It passes the data to the server through URL parameters, and the server gets the data in the web page. When passing an array, you need to convert the array to a string and pass it as a parameter.
Here's how to convert an array into a GET parameter:
$array = array("name" => "小明", "age" => 18, "gender" => "男"); $query_str = http_build_query($array);
Use the http_build_query()
function to convert an array into a query string. After calling the function, the $query_str
variable will contain the following string:
name=%E5%B0%8F%E6%98%8E&age=18&gender=%E7%94%B7
You can append this string to the URL and pass the array data with the GET method:
$url = "result.php?" . $query_str; header("Location: $url");
On the server side, you can use the $_GET
array to get the passed parameters and decode the query string into an array:
if(isset($_GET["name"]) && isset($_GET["age"]) && isset($_GET["gender"])) { $name = $_GET["name"]; $age = $_GET["age"]; $gender = $_GET["gender"]; $array = array("name" => urldecode($name), "age" => $age, "gender" => urldecode($gender)); }
Use the urldecode()
function to decode via the GET method The string passed.
The POST method passes an array which is safer than the GET method, because the data will not be passed in the URL, but will be used as the requested data. The body is passed to the server. When passing an array, you need to convert the array to JSON format and encode it into a string using the json_encode()
function.
The following is how to pass an array using the POST method:
$array = array("name" => "小明", "age" => 18, "gender" => "男"); $post_data = array("data" => json_encode($array)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://example.com/result.php"); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch);
On the server side, you can use the file_get_contents()
function to read the data passed by the POST request:
if(isset($_POST["data"])) { $json_str = $_POST["data"]; $array = json_decode($json_str, true); }
Use the json_decode()
function to decode a string into an array.
The SESSION method is a method of storing variables on the server side. Passing an array using the SESSION method requires first storing the array into the SESSION and then retrieving the stored variable on the next request.
The following is how to pass an array using the SESSION method:
$array = array("name" => "小明", "age" => 18, "gender" => "男"); session_start(); $_SESSION["my_array"] = $array;
In the next request, you can use the following code to get the array stored in SESSION:
session_start(); $array = $_SESSION["my_array"];
You can use SESSION Method passes any type of data because the SESSION variable can store an entire array object.
This article introduces several common methods of passing arrays in PHP. In practical applications, the appropriate method can be selected to transfer data according to specific needs.
The above is the detailed content of How to pass array in php. For more information, please follow other related articles on the PHP Chinese website!