In front-end development, it is often necessary to obtain data from the back-end. In back-end development, it is often necessary to convert the array into JSON format and transmit it to the front-end through the network.
As one of the most commonly used languages in back-end development, PHP provides many convenient methods to complete this task. This article will introduce how to use PHP to convert an array to JSON and pass it to the front end.
- Convert array to JSON format in PHP
PHP provides a built-in function json_encode() to convert array to JSON format. Using this function is very simple, just pass it the array to be converted as a parameter.
The following is a simple example:
$data = array( 'name' => 'John', 'age' => 30, 'gender' => 'male' ); echo json_encode($data);
The above code will output the following JSON string:
{"name":"John","age":30,"gender":"male"}
- Pass JSON data to the front end
In PHP, you can use some methods to pass JSON data to the front end. The most common method is to output a JSON string into an HTTP response. In this way, the front end can obtain JSON data through Ajax requests.
The following is a simple PHP code example for outputting JSON data:
$data = array( 'name' => 'John', 'age' => 30, 'gender' => 'male' ); header('Content-Type: application/json'); echo json_encode($data);
The above code will output the following HTTP response:
{ "name": "John", "age": 30, "gender": "male" }
Please note that we are in the header Content-Type is set to application/json. This tells the browser that the returned data is in JSON format and can be parsed correctly.
- Handling JSON data in the front-end
In the front-end, JSON data can be parsed by using the JSON object built into modern browsers. Here is a simple example of getting the JSON data from the above code and parsing it into a JavaScript object:
var xhr = new XMLHttpRequest(); xhr.open('GET', 'your-php-script.php', true); xhr.onload = function() { if (xhr.status === 200) { var data = JSON.parse(xhr.responseText); console.log(data.name); // 输出 "John" } }; xhr.send();
The above code first gets the JSON data from our PHP script via an Ajax request. We then use the JSON.parse() method to parse the JSON string into a JavaScript object. Finally, we can access the data obtained through the name attribute.
- Passing JSON data directly into JavaScript
If you don’t want to use Ajax requests to get JSON data from PHP, you can also pass JSON data directly as variables into JavaScript. The following is a simple example:
<script> var data = <?php echo json_encode($data); ?>; console.log(data.name); // 输出 "John" </script>
In this example, we convert the $data array to JSON format, and then use PHP's echo statement to output the JSON data into JavaScript code. As long as you convert the variables to JSON format in the PHP code, you can easily pass the JSON data into the JavaScript code in the front-end.
Summary
In this article, we introduced how to use PHP to convert an array into JSON format and pass it to the front-end. We also demonstrated how to parse JSON data using a modern browser's JSON object, and showed a way to pass JSON data directly into JavaScript. Hopefully you now understand how to make and process JSON data in PHP.
The above is the detailed content of How to convert php array to json and send it to the front desk. 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

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

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.
