首頁  >  文章  >  php教程  >  PHP数组传递给JavaScript以及json_encode的gbk中文乱码的解决 - jerrylsxu

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

WBOY
WBOY原創
2016-05-20 10:19:211238瀏覽

首先关于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>
复制代码

连接数据库取值给数组$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>
复制代码

数组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>
复制代码
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn