A method for assigning JS arrays in PHP

Source: Internet
Author: User

The PHP function library provides functions for encoding/decoding JSON: Json_encode () and Json_decode (), which can be used to pass arrays or objects to JavaScript in a more convenient

Because the interface side of the request, with JS processing data, so need PHP program from the database after the value assigned to the JS array.   has not found a good way, because the PHP array of data encoding and JS array encoding format is not the same, can not directly output. After a search on the Web, find a solution: PHP function Library provides the function of encoding/decoding JSON: Json_encode () and Json_decode (), can be more convenient to pass the array or object to JavaScript.   Note: More than 5.2 of PHP is bound to the JSON extension. Write in PHP as follows: The code is as follows: $arr = Array (' 1 ', Array (' 2 ', ' 3 '), Array (' new ', ' old ')); $new _arr = Json_encode ($arr); the output result of//new_arr is; ["1", ["2", "3"],["new", "old"]] echo "var data =".   $new _arr; After referencing the above PHP file in the page, the data can be operated directly in JS.

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.