Home  >  Article  >  Backend Development  >  How to convert one-dimensional array key-value pairs into urlencode format string in PHP

How to convert one-dimensional array key-value pairs into urlencode format string in PHP

PHPz
PHPzOriginal
2023-04-25 10:32:47455browse

PHP is a popular server-side scripting language commonly used in the field of web development. In web development, it is sometimes necessary to convert a one-dimensional array of key-value pairs into a string in urlencode format so that data can be passed in the URL. This article will introduce how to implement this function in PHP.

  1. urlencode function

The urlencode function in php can URL-encode a string so that it can be used for URL parameter passing. Use this function to convert special characters such as spaces and /%?&= in the parameter value into a URL-safe format.

For example:

$str = "这是一个测试的字符串";
echo urlencode($str);     //输出 %E8%BF%99%E6%98%AF%E4%B8%80%E4%B8%AA%E6%B5%8B%E8%AF%95%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2
  1. Convert one-dimensional array key-value pairs into urlencode format string

The following is the one-dimensional array key-value pair Sample code converted to a string in urlencode format:

$arr = array(
    'name' => '张三',
    'age' => 20,
    'city' => '北京'
);

$params = http_build_query($arr);
echo urlencode($params);

Parsing instructions:

  1. Create a one-dimensional array $arr, which contains 3 elements (key-value pairs);
  2. Use the PHP built-in function http_build_query to convert the one-dimensional array into a URL-encoded query string and save it in the $params variable;
  3. Use the PHP built-in function urlencode to encode the string in the $params variable It is a string in urlencode format;
  4. Output the string after urlencode.

Run the above code, the output result is as follows:

name=%E5%BC%A0%E4%B8%89&age=20&city=%E5%8C%97%E4%BA%AC

As you can see, the above code converts the key-value pairs in the one-dimensional array into a string in urlencode format, and outputs the result .

  1. Convert a string in urlencode format into a one-dimensional array key-value pair

If you need to convert a string in urlencode format into a one-dimensional array key-value pair, you can Use the PHP built-in function parse_str. The following is a sample code:

$str = "name=%E5%BC%A0%E4%B8%89&age=20&city=%E5%8C%97%E4%BA%AC";
parse_str(urldecode($str), $arr);
print_r($arr);

Parsing instructions:

  1. Create a string $str in urlencode format, which contains 3 parameters (key-value pairs);
  2. Use the PHP built-in function urldecode to decode the $str string into a normal string;
  3. Use the PHP built-in function parse_str to parse the $str string into a one-dimensional associative array and save it in the $arr variable;
  4. Use the PHP built-in function print_r to output the contents of the $arr array.

Run the above code, the output result is as follows:

Array
(
    [name] => 张三
    [age] => 20
    [city] => 北京
)

As you can see, the above code parses a string in urlencode format into a one-dimensional array key-value pair, and outputs the result .

  1. Summary

This article introduces the method of converting one-dimensional array key-value pairs into urlencode format strings in PHP, and introduces the conversion of urlencode format characters Method to convert a string into a one-dimensional array of key-value pairs. This is very useful for data passing in web development. In addition to the methods introduced in this article, there are other methods to achieve similar functions, and readers can choose according to actual needs.

The above is the detailed content of How to convert one-dimensional array key-value pairs into urlencode format string in PHP. 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