JSON uses the PHP array to the JS array, JS How to receive PHP array _php instance

Source: Internet
Author: User
First download the following file (this is a piece of code that someone else has written that specifically parses the JSON) and then introduce this file!

Http://pan.baidu.com/s/1dD8qVr7

Now when we need to use Ajax to interact with the background, how to transfer the PHP array to the JS file and JS to recognize it?

First look at the php file, when we get to $arr this array

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

Json_encode () is JSON-encoded for each value of $arr, and then we want to output a JSON array, so we add a comma after each compiled value and finally add ' [] ' to all the values, which is the format of the JSON array, Note that because we add a comma after each value is JSON encoded, this results in a comma when all the last values merge the array, all of which we have to use the substr () function to remove the last comma!

then we look at the JS file

When we use ARR to accept a JSON array of PHP file transfers

var json = Json.parse (arr);

JSON is the object we started to download in that file, and we use its Parse method to convert the JSON array into a JS array! This is the variable JSON is received by a JS array so directly can not print out, you can traverse this JSON array or json[0] to output!

In fact, the idea of converting PHP arrays into JS arrays is to make use of this intermediate amount of JSON! Of course, you can only use PHP and JS to achieve the conversion of arrays, more than one way!

Conversions between PHP arrays and JSON

JSON is used in many cases because of the interaction between the program and the JS function when using an Ajax object. Because JS does not know the array in PHP, PHP does not know the array or object in JS. JSON is a good solution to this problem.

Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of JavaScript, which means that JavaScript can read JSON directly and is very handy.

The specific form of JSON is:

1. Objects

An object is an unordered collection of "name/value pairs". An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pair ' is separated by", "(comma).

For example: {"username": "Eric", "age": +, "sex": "Man"}

code example:

2. Arrays

An array is an ordered collection of values (value). An array begins with "[" (the left square bracket), and "]" (the right square bracket) ends. Use "," (comma) to separate values.

For example: ["Eric", "+", "man"]

code example:

Note: Objects and arrays are not the same as the two forms in JS, the object with "." Called, the array is called with subscript [0], [1]. Also note that string-type values are enclosed in quotation marks when the JSON string is passed.

Converting arrays into JSON in PHP

Powerful PHP already provides built-in functions: Json_encode () and Json_decode (). It is easy to understand that Json_encode () is the conversion of PHP arrays into JSON. Instead, Json_decode () converts the JSON into a PHP array.

For example:

$array = Array ("name" = "Eric", "age" = 23); echo Json_encode ($array);

The program will print out:

Copy the Code code as follows:
{"Name": "Eric", "Age": 23}

Let's look at the following example:

$array = Array (0 = "Eric", 1 = 23); echo Json_encode ($array);

The program will print out: ["Eric", 23]

As you can see from the two examples above, if the keys of the PHP array are numbers, then Json_encode () returns a JSON in the form of an array, if the keys of the PHP array are all strings. Then Json_encode () returns a JSON in the form of an object. Just already said. The invocation of the two in JS is different.

In fact, as long as there is a string form key in the PHP array's key, then Json_encode () returns the JSON in the form of the object. This is not true. Because, although there is no error in PHP code, if you pass such a JSON to the JS function, JS will use this JSON as an object, and the object is not possible to use a number as the property name. That is to say JS do not know what this is: User.0.username (the middle is the number 0)

  • Related Article

    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.