PHP Two-dimensional array passed to JS problem solving record

Source: Internet
Author: User

Demand:

PHP read from the database to a two-dimensional array, passed to JS


Implementation steps:

Php:json_encode→json→js:eval

That is, using Json_encode () in PHP to convert a two-dimensional PHP array into JSON format, passed to JS. Use eval() to parse the two-dimensional array of JS.


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 ("A", "B", "C", "All-in-a-D"), Array ("Q", "W", Up)); $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 encountered:

When a two-dimensional array in 1.php is converted to JSON using json_encode () . The second-dimensional array can echo out. But the conversion to JSON is empty.

Check the data online to determine the problem is that the second-dimensional array contains non-UTF8 encodings.

Sure enough, I switched the second-dimensional array in the two-dimensional array to the first array with the JSON empty.

Conclusion:json_encode () can convert multidimensional arrays, but the basic requirement is to encode to UTF8. Encountering a sub-array conversion json in a multidimensional array is null, and it is very likely that this subarray contains elements that are not UTF8 encoded.


2.wamp MySQL in phpmyadmin see the data table content is normal Chinese characters, but in PHP read out printing found that Chinese characters have become? The

Each table in the database is organized in a utf8_general_ci manner. The PHP file also declares

Header ("content-type:text/html; Charset=utf-8 ") Mysqli_query ($con," set character set ' Utf-8 ' "); Mysqli_query ($con," Set names ' Utf-8 ' ");
This code I have always been so used (before the use of the Wamp low version number, or the use of the mysql_query, now that the error is discarded, changed to mysqli), has not appeared before the Chinese become? The situation. Online a check, the original is utf-8 and UTF8 this place ...

This should be the case:

Header ("content-type:text/html; Charset=utf8 ") Mysqli_query ($con," set character set ' UTF8 ' "); Mysqli_query ($con," Set names ' UTF8 ' ");
Conclusion: UTF8 is still used in MySQL.

。。

No words.




PHP Two-dimensional array passed to JS problem solving record

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.