Php 2D array passed to js issue resolution record
Requirements:
Php reads two-dimensional arrays from the database and passes them to js.
Steps:
Php: json_encode → json → js: eval
In php, json_encode () is used to convert the two-dimensional array of php into json format, transfer it to js, and parse the two-dimensional array of js using eval.
Code:
Php:
var json=
;
Js:
<Script type = "text/javascript" src = "http ://... /test. php "> </script> <script language =" javascript "type =" text/javascript "> $ (document ). ready (function () {var jsonstr = eval (json); for (var k = 0; k
Problems:
1. When the two-dimensional array in php is converted to json using json_encode (), the two-dimensional array can be echo, but the converted to json is empty.
Check the information on the Internet to confirm that the second-dimensional array contains non-utf8 encoding. Sure enough, I changed the interchange position of the Second-dimensional array in the two-dimensional array to the json value of the first array.
Conclusion: json_encode () can be used to convert multi-dimensional arrays, but the encoding is UTF-8. When a subarray in a multi-dimensional array converts json to null, it is very likely that this subarray contains elements encoded instead of UTF-8.
2. wamp mysql shows that the data table content in phpmyadmin is normal Chinese characters, but it can be read and printed in php? .
The sorting method of each table in the database is utf8_general_ci, which is also stated in the PHP file.
header("Content-Type: text/html; charset=utf-8") ;...mysqli_query($con,"set character set 'utf-8'");mysqli_query($con,"set names 'utf-8'");
I have always used these codes (I used low wamp versions or mysql_query). Now I have replaced mysqli with an error ), I haven't seen any Chinese changes before? . Check on the Internet. It turns out to be UTF-8 and utf8...
It should be like this:
header("Content-Type: text/html; charset=utf8") ;...mysqli_query($con,"set character set 'utf8'");mysqli_query($con,"set names 'utf8'");
Conclusion: Use utf8 in mysql... Speechless.