PHP Two-dimensional array passed to JS problem solving record

Source: Internet
Author: User

Demand:

PHP reads a two-dimensional array from the database. Pass to JS


Implementation steps:

Php:json_encode→json→js:eval

That is , use Json_encode () in PHP to convert a two-dimensional PHP array into JSON format. Passed to JS, using 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 it turns into JSON 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 coding for 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 sees data table contents in phpMyAdmin as normal kanji. But using PHP to read out the 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 ' ");
That's what I used to do with the code (previously, the Wamp low version number. Still use the mysql_query. Now the error says abandoned, changed to mysqli). Have you ever seen Chinese before? The situation. Online A search, originally 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: MySQL is the use of UTF8 bar ...

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.