标签:
首先关于json_encode的gbk中文乱码,创建JSON函数,这一段来自网上某一位大侠
/************************************************************** * * 使用特定function对数组中所有元素做处理 * @param string &$array 要处理的字符串 * @param string $function 要执行的函数 * @return boolean $apply_to_keys_also 是否也应用到key上 * @access public * *************************************************************/ function arrayRecursive(&$array, $function, $apply_to_keys_also = false) { foreach ($array as $key => $value) { if (is_array($value)) { arrayRecursive($array[$key], $function, $apply_to_keys_also); } else { $array[$key] = $function($value); } if ($apply_to_keys_also && is_string($key)) { $new_key = $function($key); if ($new_key != $key) { $array[$new_key] = $array[$key]; unset($array[$key]); } } } } /************************************************************** * * 将数组转换为JSON字符串(兼容中文) * @param array $array 要转换的数组 * @return string 转换得到的json字符串 * @access public * *************************************************************/ function JSON($array) { arrayRecursive($array, ‘urlencode‘, true); $json = json_encode($array); return urldecode($json); }
连接数据库取值给数组$array1
$dbcnx = @mysql_connect ( "localhost", "root", "1234" ); if (! $dbcnx) { echo ("Unable to connect to the " . "database server at this time."); exit (); } if (! @mysql_select_db ( "pms" )) { echo ("Unable to locate the joke " . "database at this time."); exit (); } mysql_query ( "SET NAMES ‘GB2312‘" ); $q=mysql_query("select * from ability where ALV = 1"); while($row=mysql_fetch_array($q)){ $array1[] = $row[AName]; }
数组array1传递到JavaScript给数组ability1
<script type="text/javascript" src="JS/jquery-1.7.2.min.js"></script> <script type="text/javascript"> var ability1=<?php echo JSON($array1);?>; var a=eval("ability1"); alert(a[0]); </script>
PHP数组传递给JavaScript以及json_encode的gbk中文乱码的解决
标签:
原文地址:http://www.cnblogs.com/shouce/p/5445021.html