Convert PHP array to JS array, JS How to receive PHP array, JSON usage, jsjson_php tutorial

Source: Internet
Author: User

The PHP array to the JS array, JS how to receive PHP arrays, JSON usage, Jsjson


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 ($arras$value) {    $json . = Json_encode ($value). ', 'echosubstr($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!

Reprint Please specify source: http://www.ly89.cn/detailB/44.html

http://www.bkjia.com/PHPjc/1059469.html www.bkjia.com true http://www.bkjia.com/PHPjc/1059469.html techarticle the PHP array to the JS array, JS How to receive the PHP array, JSON usage, Jsjson first download the following file (this is someone else to write the code specifically parsing JSON), and then introduce this ...

  • 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.