Because the interface requires processing data in js, the php program needs to retrieve the value from the database and assign the value to the js array. We have never found a good solution because the data encoding format of PHP arrays is different from that of JS arrays and cannot be directly output.
After searching for a message on the Internet, find the solution:
The PHP function library provides JSON encoding/decoding functions: json_encode () and json_decode (), which can easily pass arrays or objects to javascript. Note: PHP 5.2 and later versions are bound with JSON extensions.
Write in php as follows:
Copy codeThe 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 on the page, you can directly operate on data in js.