How to pass array in php

王林
Release: 2023-05-19 20:29:06
Original
1319 people have browsed it

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.

  1. GET method passes array

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

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

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");
Copy after login

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));
}
Copy after login

Use the urldecode() function to decode via the GET method The string passed.

  1. The POST method passes an array

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

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);
}
Copy after login

Use the json_decode() function to decode a string into an array.

  1. SESSION method passes 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;
Copy after login

In the next request, you can use the following code to get the array stored in SESSION:

session_start();
$array = $_SESSION["my_array"];
Copy after login

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template