JSON usage: convert PHP arrays to JS arrays. how does JS receive PHP arrays?

Source: Internet
Author: User
This article describes how to convert a PHP array to a JS array in JSON format. how does JS receive relevant information about a PHP array, if you need this file, you can download the following file (this is a code written by someone else to parse 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 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!

The code is 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]

The above two examples show that if the keys of the PHP array are numbers, then json_encode () returns an array of Json, if the keys of the PHP array are all strings. Then json_encode () returns an object-type Json. I already said. The two are called differently in js.

In fact, as long as there is a string key in the PHP array key, then json_encode () will return the Json in the object format. This is incorrect. Because, although PHP code does not produce errors, if such Json is passed to the JS function, JS regards this Json as an object, however, objects cannot use numbers as attribute names. That is to say, JS does not know what this is: user.0.username (in the middle is the number 0)

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.