php 中使用 json_encode() 內(nèi)置函數(shù)(php 5.2)可以使用得 php 中數(shù)據(jù)可以與其它語言很好的傳遞并且使用它。這個(gè)函數(shù)的功能是將數(shù)值轉(zhuǎn)換成json數(shù)據(jù)存儲格式
成都地區(qū)優(yōu)秀IDC服務(wù)器托管提供商(創(chuàng)新互聯(lián)).為客戶提供專業(yè)的德陽服務(wù)器托管,四川各地服務(wù)器托管,德陽服務(wù)器托管、多線服務(wù)器托管.托管咨詢專線:13518219792
$arr = array ('a'=1,'b'=2,'c'=3,'d'=4,'e'=5);
echo json_encode($arr);
//結(jié)果
//{"a":1,"b":2,"c":3,"d":4,"e":5}
/*
下面看一款json_encode中文亂碼問題
解決方法是用urlencode()函數(shù)處理以下,在json_encode之前,把所有數(shù)組內(nèi)所有內(nèi)容都用urlencode()處理一下,然用json_encode()轉(zhuǎn)換成json字符串,最后再用urldecode()將編碼過的中文轉(zhuǎn)回來
*/
function arrayrecursive($array, $function, $apply_to_keys_also = false)
{
static $recursive_counter = 0;
if (++$recursive_counter 1000) {
die('possible deep recursion attack');
}
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]);
}
}
}
$recursive_counter--;
}
/**************************************************************
*
* 將數(shù)組轉(zhuǎn)換為json字符串(兼容中文)
* @param array $array 要轉(zhuǎn)換的數(shù)組
* @return string 轉(zhuǎn)換得到的json字符串
* @access public
*
*************************************************************/
function json($array) {
arrayrecursive($array, 'urlencode', true);
$json = json_encode($array);
return urldecode($json);
}
$array = array
(
'name'='希亞',
'age'=20
);
echo json($array);
//應(yīng)用實(shí)例
$servname="localhost";
$sqlservname="root";
$sqlservpws="123456";
$sqlname="lock1";
$db=mysql教程_connect($servname,$sqlservname,$sqlservpws) or die("數(shù)據(jù)庫教程連接失敗");
mysql_select_db($sqlname,$db);
$sql = "select * from t_operater";
$result =mysql_query($sql);
$rows = mysql_num_rows($result);
while($obj = mysql_fetch_object($result))
{
$arr[] = $obj;
}
echo '({"total":"'.$rows.'","results":'.json_encode($arr).'})';
connect之後,set charset為utf-8
?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* change character set to utf8 */
if (!$mysqli-set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli-error);
} else {
printf("Current character set: %s\n", $mysqli-character_set_name());
}
$mysqli-close();
?
header("Content-Type:text/html;charset=UTF-8");
$arr=array('1','2','三');
echo?$arr['2']."br?/";
print_r($arr);
echo?"br?/";
print_r(json_encode($arr));
echo?"br?/";
print_r(json_decode(json_encode($arr)));
輸出json數(shù)據(jù)會有?但是?翻譯成數(shù)組后就行了
解決辦法是在使用json_encode之前把字符用函數(shù)urlencode()處理一下,然后再json_encode,輸出結(jié)果的時(shí)候在用函數(shù)urldecode()轉(zhuǎn)回來。具體如下:
//urlencode處理
function?json_array($obj)?{
foreach($obj?as?$key?=?$value)?{
if(is_array($obj[$key]))?{
$obj[$key]?=?json_array($obj[$key]);
}else{
$obj[$key]?=?urlencode($value);
}
}
return?$obj;
}
//urldecode解密轉(zhuǎn)換為中文字符
function?_json($obj)?{
return?urldecode(json_encode(json_array($obj)));
}
echo?_json($json);//$json為你要輸出的json數(shù)組
此處遞歸調(diào)用是為了處理多維數(shù)組······
把數(shù)據(jù)庫編碼,網(wǎng)頁編碼,網(wǎng)頁顯示編碼,都改成一致的試試!