Home  >  Article  >  Backend Development  >  How to convert request parameters from php array

How to convert request parameters from php array

PHPz
PHPzOriginal
2023-04-19 09:15:27658browse

In PHP development, we often need to convert arrays into request parameters for data transmission. Converting PHP arrays into request parameters is a very common operation and an inevitable operation in PHP development. This article will introduce how to use functions in PHP to convert arrays into request parameters.

1. Use the http_build_query function

PHP has a built-in http_build_query function, which can be used to convert arrays into request parameters. The http_build_query function is used to convert an array into a query string after URL-encoding. Its basic syntax is as follows:

string http_build_query ( mixed $query_data [, string $numeric_prefix [, string $arg_separator [, int $enc_type = PHP_QUERY_RFC1738 ]]] )

Among them, query_data represents the array to be converted, and numeric_prefix is ​​optional, which means that the array is forced to be converted into The key name of a pure numeric index, arg_separator represents the separator between array elements, enc_type represents which character set is used for URL encoding, and the default is PHP_QUERY_RFC1738.

For example, we have the following array:

$data = array(
    'name' => 'Tom',
    'age' => 20,
    'gender' => 'male'
);

We can use the http_build_query function to convert it into request parameters in the following format:

name=Tom&age=20&gender=male

The specific usage is as follows:

$data = array(
    'name' => 'Tom',
    'age' => 20,
    'gender' => 'male'
);
$params = http_build_query($data);
echo $params;

2. Handwritten array to request parameter function

If we don’t want to use PHP’s built-in function, we can also handwrite a function to convert the array into request parameter. The following is a simple sample code:

function array2param($data)
{
    $params = array();
    foreach ($data as $key => $value) {
        if (is_array($value)) {
            $params[] = array2param($value);
        } else {
            $params[] = urlencode($key) . '=' . urlencode($value);
        }
    }
    return implode('&', $params);
}

We can call this function to convert the array into request parameters. The sample code is as follows:

$data = array(
    'name' => 'Tom',
    'age' => 20,
    'gender' => 'male'
);
$params = array2param($data);
echo $params;

The output result is as follows:

name=Tom&age=20&gender=male

3. Use http_build_query and array recursive traversal

Using http_build_query and array recursive traversal can be more convenient when processing multi-dimensional arrays. The following is the sample code:

function array2param($data) {
    $queryData = array();       // 将生成的字符串数组
    foreach ($data as $key => $value) {
        if (is_array($value)) {
            $queryData[] = http_build_query(array($key => $value));  // 处理多维数组
        } else {
            $queryData[] = urlencode($key) . '=' . urlencode($value);
        }
    }
    return implode('&', $queryData);
}

Usage example:

$data = array(
    'name' => 'Tom',
    'age' => 20,
    'gender' => 'male',
    'contact' => array(
        'email' => 'example@mail.com',
        'phone' => '1234567890'
    )
);
$params = array2param($data);
echo $params;

Output result:

name=Tom&age=20&gender=male&contact%5Bemail%5D=example%40mail.com&contact%5Bphone%5D=1234567890

Summary

In PHP, converting an array into a request parameter is A very common operation. This article introduces three different implementation methods, namely using http_build_query function, handwritten array to request parameter function and using http_build_query and array recursive traversal. Depending on the needs and actual situation, different methods can be chosen to handle it.

The above is the detailed content of How to convert request parameters from php array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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