Home > Backend Development > PHP Tutorial > PHP two-dimensional array is passed to js problem solving record_PHP tutorial

PHP two-dimensional array is passed to js problem solving record_PHP tutorial

WBOY
Release: 2016-07-13 10:17:42
Original
931 people have browsed it

php two-dimensional array is passed to js problem solving record

Requirements:

php reads the two-dimensional array from the database and passes it to js


Implementation steps:

php:json_encode → json → js:eval

That is, use json_encode() in php to convert the php two-dimensional array into json format, pass it to js, ​​and use eval() to parse to get the js two-dimensional array.


Code:

php:

<?php  
header("Content-Type: text/html; charset=utf8") ;
$con=mysqli_connect("url","name","password","databasename");
// Check connection
if (mysqli_connect_errno($con))
    echo "Failed to connect to MySQL: " . mysqli_connect_error();

mysqli_query($con,"set character set &#39;utf8&#39;");
mysqli_query($con,"set names &#39;utf8&#39;");

$json_arr = array(array("a","b","c",1,2,3),array("q","w",1,2));
$jsonstr = json_encode($json_arr);

?>
var json=<?=$jsonstr?>;
Copy after login

Copy after login
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>
Copy after login

Problems encountered:

1. When the two-dimensional array in php is converted into json using json_encode(), the second-dimensional array can be echoed out, but the converted json is empty.

Check the information online to determine that the problem is that the second-dimensional array contains non-utf8 encoding. Sure enough, I swapped the positions of the second-dimensional array in the two-dimensional array, and the json of the first array became empty.

Conclusion: json_encode() can convert multi-dimensional arrays, but the basic requirement is to encode it into utf8. When a sub-array in a multi-dimensional array is converted from json to null, it is very likely that this sub-array contains elements whose encoding is not utf8.


2.wamp mysql In phpmyadmin, I see that the content of the data table is normal Chinese characters, but when I read it and print it with php, I found that the Chinese characters have changed? .

The organization method of each table in the database is utf8_general_ci, which is also declared in the php file

header("Content-Type: text/html; charset=utf-8") ;
...
mysqli_query($con,"set character set &#39;utf-8&#39;");
mysqli_query($con,"set names &#39;utf-8&#39;");
Copy after login
I have always used these codes like this (I used a low version of wamp before, or mysql_query, but now the error says it is abandoned, so I changed it to mysqli). There has never been a Chinese change before? situation. After checking online, it turned 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 &#39;utf8&#39;");
mysqli_query($con,"set names &#39;utf8&#39;");
Copy after login
Conclusion: Let’s use utf8 in mysql. . . Speechless.



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/890205.htmlTechArticlephp passes the two-dimensional array to js problem solving record requirements: php reads the two-dimensional array from the database and passes it Implement steps in js: php:json_encode → json → js:eval That is, in ph...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template