JSON usage: convert PHP array to JS array. how does JS receive PHP array? jsonjs_PHP tutorial

Source: Internet
Author: User
JSON usage: convert PHP array to JS array. how does JS receive PHP array and jsonjs. JSON usage: convert PHP array to JS array. how does JS receive PHP array? jsonjs first downloads the following file (this is a piece of code written by someone else to parse json ), then introduce this JSON usage to convert the PHP array to the JS array. how does JS receive the PHP array?

First download the following file (this is a code written by someone else to parse json), and then introduce this file!


Now, when we need to use ajax to interact with the background, how can we transmit php arrays to js files and identify them by js?

First Read the php file. when we get the $ arr array

foreach ($arr as $value) {  $json .= json_encode($value) . ',';} echo '[' . substr($json,0,strlen($json) - 1) . ']';

Json_encode () is to encode every value of $ arr in json format, and then we want to output a json Array, therefore, we add a comma after each compiled value and add '[]' to all values. this is the format of the json Array, note that a comma is added after each value is encoded in json format, which leads to an additional comma when all values are merged into the array. substr () is used for all values () remove the last comma from the function!

Then let's look at the js file.

After we use arr to receive the json Array transmitted to the PHP file

var json = JSON.parse(arr);

JSON is an object defined in the file we downloaded. We use its parse method to convert the json Array into an array of js! This variable json receives a js array, so it cannot be printed directly. you can traverse this json Array or json [0] to output it!

In fact, the idea of converting a php array into a js array is to use the intermediate volume of json! Of course, you can also use php and js to convert arrays. there are more than one method!

Conversion between PHP arrays and Json

Json is often used because of data interaction between programs and JS functions when ajax objects are used. Because JS does not know arrays in PHP, PHP does not know arrays or objects in JS. Json solves this problem.

Introduction to Json

JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of JavaScript, which means JavaScript can directly read Json, which is very convenient.

The Json format is as follows:

1. object

An object is an unordered set of 'name/value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs.

Example: {"username": "Eric", "age": 23, "sex": "man "}

Sample code:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.