Heim > php教程 > php手册 > Hauptteil

PHP数组传递给JavaScript以及json_encode的gbk中文乱码的解决 - jerrylsxu

WBOY
Freigeben: 2016-05-20 10:19:21
Original
1235 Leute haben es durchsucht

首先关于json_encode的gbk中文乱码,创建JSON函数,这一段来自网上某一位大侠

复制代码
<span style="color: #008000;">/*</span><span style="color: #008000;">*************************************************************
 *
*    使用特定function对数组中所有元素做处理
*    @param    string    &$array        要处理的字符串
*    @param    string    $function    要执行的函数
*    @return boolean    $apply_to_keys_also        是否也应用到key上
*    @access public
*
************************************************************</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">function</span> arrayRecursive(&<span style="color: #800080;">$array</span>, <span style="color: #800080;">$function</span>, <span style="color: #800080;">$apply_to_keys_also</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">)
{
    </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$array</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) {
        </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$value</span><span style="color: #000000;">)) {
            arrayRecursive(</span><span style="color: #800080;">$array</span>[<span style="color: #800080;">$key</span>], <span style="color: #800080;">$function</span>, <span style="color: #800080;">$apply_to_keys_also</span><span style="color: #000000;">);
        } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
            </span><span style="color: #800080;">$array</span>[<span style="color: #800080;">$key</span>] = <span style="color: #800080;">$function</span>(<span style="color: #800080;">$value</span><span style="color: #000000;">);
        }

        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$apply_to_keys_also</span> && <span style="color: #008080;">is_string</span>(<span style="color: #800080;">$key</span><span style="color: #000000;">)) {
            </span><span style="color: #800080;">$new_key</span> = <span style="color: #800080;">$function</span>(<span style="color: #800080;">$key</span><span style="color: #000000;">);
            </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$new_key</span> != <span style="color: #800080;">$key</span><span style="color: #000000;">) {
                </span><span style="color: #800080;">$array</span>[<span style="color: #800080;">$new_key</span>] = <span style="color: #800080;">$array</span>[<span style="color: #800080;">$key</span><span style="color: #000000;">];
                </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$array</span>[<span style="color: #800080;">$key</span><span style="color: #000000;">]);
            }
        }
    }
}

</span><span style="color: #008000;">/*</span><span style="color: #008000;">*************************************************************
 *
*    将数组转换为JSON字符串(兼容中文)
*    @param    array    $array        要转换的数组
*    @return string        转换得到的json字符串
*    @access public
*
************************************************************</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">function</span> JSON(<span style="color: #800080;">$array</span><span style="color: #000000;">) {
    arrayRecursive(</span><span style="color: #800080;">$array</span>, 'urlencode', <span style="color: #0000ff;">true</span><span style="color: #000000;">);
    </span><span style="color: #800080;">$json</span> = json_encode(<span style="color: #800080;">$array</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">urldecode</span>(<span style="color: #800080;">$json</span><span style="color: #000000;">);
}</span>
Nach dem Login kopieren
复制代码

连接数据库取值给数组$array1

复制代码
<span style="color: #800080;">$dbcnx</span> = @<span style="color: #008080;">mysql_connect</span> ( "localhost", "root", "1234"<span style="color: #000000;"> );
</span><span style="color: #0000ff;">if</span> (! <span style="color: #800080;">$dbcnx</span><span style="color: #000000;">) {
    </span><span style="color: #0000ff;">echo</span> ("Unable to connect to the " . "database server at this time."<span style="color: #000000;">);
    </span><span style="color: #0000ff;">exit</span><span style="color: #000000;"> ();
}

</span><span style="color: #0000ff;">if</span> (! @<span style="color: #008080;">mysql_select_db</span> ( "pms"<span style="color: #000000;"> )) {
    </span><span style="color: #0000ff;">echo</span> ("Unable to locate the joke " . "database at this time."<span style="color: #000000;">);
    </span><span style="color: #0000ff;">exit</span><span style="color: #000000;"> ();
}

</span><span style="color: #008080;">mysql_query</span> ( "SET NAMES 'GB2312'"<span style="color: #000000;"> );

    </span><span style="color: #800080;">$q</span>=<span style="color: #008080;">mysql_query</span>("select * from ability where ALV = 1"<span style="color: #000000;">); 
    </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$row</span>=<span style="color: #008080;">mysql_fetch_array</span>(<span style="color: #800080;">$q</span><span style="color: #000000;">)){ 
     </span><span style="color: #800080;">$array1</span>[] = <span style="color: #800080;">$row</span><span style="color: #000000;">[AName];
}</span>
Nach dem Login kopieren
复制代码

数组array1传递到JavaScript给数组ability1

复制代码
<script type="text/javascript" src="JS/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
<span style="color: #0000ff;">var ability1=<?php echo JSON($array1);?><span style="color: #000000;">;
<span style="color: #0000ff;">var a=eval("ability1"<span style="color: #000000;">);
alert(a[0]);
</script>
Nach dem Login kopieren
复制代码
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage