Php 2D array passed to js issue resolution record

Source: Internet
Author: User

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.



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.