How to convert array to json format in php

PHPz
Release: 2023-04-04 12:24:01
Original
501 people have browsed it

PHP is a powerful programming language widely used in web development, while JSON is a lightweight data exchange format. In web development, we often need to convert arrays in PHP into JSON format to facilitate dynamic loading and display on the front-end page. So, how to convert PHP array to JSON? This article will introduce it to you in detail.

What is JSON?

JSON, the full name of JavaScript Object Notation, is a lightweight data exchange format. It is based on some syntax features of JavaScript and is used to transmit structured data over the network. JSON format data is stored in the form of key-value pairs and supports composite data types such as numbers, strings, Boolean values, arrays, and objects.

The following is an example of JSON format data:

{
   "name": "John",
   "age": 30,
   "city": "New York",
   "hobbies": ["reading", "writing", "swimming"]
}
Copy after login

Convert PHP array to JSON format

In PHP, we can use the built-in json_encode() Function converts PHP array to JSON format. json_encode() The function converts a PHP variable into a JSON format string.

The following is an example of converting a PHP array to JSON format:

 'John', 'age' => 30, 'city' => 'New York');
$jsonData = json_encode($data);
echo $jsonData;
?>
Copy after login

The above code will output the following results:

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

In the above example, we use Use the array() function in PHP to create a simple associative array, and then use the json_encode() function to convert it to a JSON format string. It is worth noting that json_encode() will encode Unicode characters into the UTF-8 character set by default.

Other common usage

Using JSON in HTML

In the front-end page, we can use JavaScript to parse the JSON data generated through PHP for display on the page and dynamic updates. The following is an example of using JSON in HTML:



   
      JSON Example
      
   
          

Name: 

      

Age: 

      

City: 

          
Copy after login

Processing JSON objects

In PHP, we can also use the json_decode() function to convert a JSON-formatted Convert string to PHP array or object. This function also supports decoding JSON into multiple data types, such as arrays, objects, integers, floating point, etc. The following is an example of converting a JSON object into a PHP object:

name; // 输出: "John"
?>
Copy after login

In the above example, we use the json_decode() function to decode a JSON formatted string into a PHP object , and then access the object's properties via $phpObj->name.

Conclusion

In web development, converting PHP arrays to JSON format data is a very basic but important task. Through the introduction of this article, I believe readers can already master how to use the json_encode() function to convert a PHP array into a JSON format string, and use JSON data for dynamic updates on the front-end page. At the same time, we also introduced the common usage of using the json_decode() function to decode JSON data into a PHP array or object.

The above is the detailed content of How to convert array to json format 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!