How to convert array to JSON in php and retain key

PHPz
Release: 2023-03-28 17:05:40
Original
1342 people have browsed it

In PHP development, converting arrays into JSON format data is a very common operation. However, during the conversion process, sometimes it is necessary to preserve the keys in the array from being lost. This article will introduce two methods to convert a PHP array into JSON and retain the key.

Method 1: Use the json_encode() function

The json_encode() function is the main function in PHP to convert arrays into JSON. By default, this function will lose the key in the array and only retain the value. However, we can use the JSON_FORCE_OBJECT option to force the array to be converted to an object and retain the key.

Sample code:

//一个简单的关联数组
$arr = array(
    "name" => "John",
    "age" => 30,
    "city" => "New York"
);
 
//将数组转换成JSON并强制转换成object
$json = json_encode($arr, JSON_FORCE_OBJECT);
 
//输出JSON字符串
echo $json;
Copy after login

Running result:

{"name":"John","age":30,"city":"New York"}
Copy after login

As you can see, the converted JSON string retains the keys in the array.

Method 2: Use the json_decode() function

In addition to the json_encode() function, we can also use the json_decode() function to convert the JSON string back to a PHP array, and Keep the original key. It should be noted that the json_decode() function converts a JSON string into a PHP object by default, so the second parameter needs to be set to true to convert it into an associative array.

Sample code:

//一个JSON字符串
$json = '{"name":"John","age":30,"city":"New York"}';
 
//将JSON字符串转换成数组并保留key
$arr = json_decode($json, true);
 
//输出数组
print_r($arr);
Copy after login

Running results:

Array
(
    [name] => John
    [age] => 30
    [city] => New York
)
Copy after login

Summary:

This article introduces two methods to implement PHP arrays Convert to JSON and keep the key. Using the json_encode() function to convert the array into an object and retaining the key is a simpler method, while using the json_decode() function to convert the JSON string back to a PHP array and retaining the key can better meet some needs to first convert to JSON and then convert back. PHP array scenario. No matter which method is used, you can easily convert the PHP array into JSON and retain the key, which provides developers with unlimited convenience.

The above is the detailed content of How to convert array to JSON in php and retain key. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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