"Php+js" JSON usage of the PHP array to JS array, JS How to receive the PHP array

Source: Internet
Author: User

1. I need to accept JSON array data in the following format to render

[{date: "2018-06-09", Data: "1"}, {date: "2018-06-23", Data: "Aadsf34"}, {date: "2018-07-02", Data: "3"},]

2, server PHP How to output can be JS parsed into the above data?

PHP Output: (String)

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!

3. then we look at the JS file

After we have accepted the JSON array to the PHP file transfer with arr:(foreground JSON parses the string into a JSON array)

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!

"Php+js" JSON usage of the PHP array to JS array, JS How to receive the PHP array

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.