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;
Running result:
{"name":"John","age":30,"city":"New York"}
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);
Running results:
Array ( [name] => John [age] => 30 [city] => New York )
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor
