Home > Backend Development > PHP Problem > How to convert between arrays and URL parameter strings in php

How to convert between arrays and URL parameter strings in php

PHPz
Release: 2023-04-19 13:54:00
Original
1065 people have browsed it

In web development, we often need to convert arrays into URL parameter strings for data transfer between pages or parameter transfer in API interface requests. In PHP, we can convert between arrays and URL parameter strings through some built-in functions. Next, I will introduce some common methods to achieve this function.

1. Convert an array into a URL parameter string

  1. http_build_query() function

The http_build_query() function can convert an array into a URL parameter character string. The syntax of this function is as follows:

string http_build_query ( mixed $query_data , string $numeric_prefix = "" , string $arg_separator = "" , int $enc_type = PHP_QUERY_RFC1738 )
Copy after login

Among them, the query_data parameter is an associative array or numerical index array containing the data to be converted, and the numeric_prefix parameter is the optional string prefix required for the numerical index array. The default is empty. String, the arg_separator parameter is an optional parameter separator, the default value is "&", the enc_type parameter is an optional string format, the default is PHP_QUERY_RFC1738.

Sample code:

<?php
$data = array(
   &#39;user&#39; => 'admin',
   'email' => 'admin@example.com',
   'age' => 18
);
$query_string = http_build_query($data);
echo $query_string;
?>
Copy after login

The output result is:

user=admin&email=admin%40example.com&age=18
Copy after login
  1. Custom parameter separator and prefix

Sample code:

<?php
$data = array(
   &#39;user&#39; => 'admin',
   'email' => 'admin@example.com',
   'age' => 18
);
$query_string = http_build_query($data, 'id_', '|');
echo $query_string;
?>
Copy after login

The output result is:

id_user=admin|id_email=admin%40example.com|id_age=18
Copy after login
  1. urlencode() function

The urlencode() function can encode the URL parameter string and special characters. escape. Sample code:

<?php
$data = array(
   &#39;user&#39; => 'admin',
   'email' => 'admin@example.com',
   'age' => 18
);
$query_string = http_build_query($data);
$query_string = urlencode($query_string);
echo $query_string;
?>
Copy after login

The output result is:

user%3Dadmin%26email%3Dadmin%2540example.com%26age%3D18
Copy after login

2. Convert the URL parameter string into an array

  1. parse_str() function

The parse_str() function can parse URL parameter strings into associative arrays. Sample code:

Copy after login

The output result is:

Array
(
    [user] => admin
    [email] => admin@example.com
    [age] => 18
)
Copy after login
Copy after login
  1. Use the explode() function to manually parse

If the http_build_query() function is not used to generate URL parameters Strings can also be parsed manually using the explode() function. Sample code:

Copy after login

The output result is:

Array
(
    [user] => admin
    [email] => admin@example.com
    [age] => 18
)
Copy after login
Copy after login

3. Summary

The above is to convert a PHP array into a URL parameter string and convert a URL parameter string into an array Methods. In actual development, we can flexibly use these methods to realize data transfer and parameter transfer in API interface requests.

The above is the detailed content of How to convert between arrays and URL parameter strings 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