Php 2D array passed to js issue resolution record, 2D array js
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:
<?php header("Content-Type: text/html; charset=utf8") ;$con=mysqli_connect("url","name","password","databasename");// Check connectionif (mysqli_connect_errno($con)) echo "Failed to connect to MySQL: " . mysqli_connect_error();mysqli_query($con,"set character set 'utf8'");mysqli_query($con,"set names 'utf8'");$json_arr = array(array("a","b","c",1,2,3),array("q","w",1,2));$jsonstr = json_encode($json_arr);?>var json=<?=$jsonstr?>;
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<jsonstr.length;k++){ for(var i=0;i<jsonstr[k].length;i++) alert(jsonstr[k][i]); } })</script>
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.
A two-dimensional array passed by the php page is output cyclically in js.
To convert it into a js object, we recommend that the json_encode output be in json format, and then the js end is parsed as a js object, so that you can for (var a in data ){
For (var B in data [a]) {
// B is the third key
// Data [a] [B] is a value
}
}
How to transfer a two-dimensional Js array to the php background?
Js uses a two-dimensional array to save the data. To submit the data to the background directly, submit the data to the background as a string: string '5, 3.99, 3.99, '(length = 15) go to the computer anti-virus forum to view the answer details>