php轉換json時, 其中的中文會轉換成Unicode, 要保持原來中文的意思php
/** * 將數組裏面帶有中文的字串保留以JSON格式返回 * * @param array $arr 數組 * @return string JSON格式的字符串 */ function toJson($arr) { $ajax = ToUrlencode($arr); $str_json = json_encode($ajax); return urldecode($str_json); } /** * 將數組裏面帶有中文的字串用urlencode轉換格式返回 * * @param array $arr 數組 * @return array */ function ToUrlencode($arr) { $temp = array(); if (is_array($arr)) { foreach ($arr AS $key => $row) { $temp[$key] = $row; if (is_array($temp[$key])) { $temp[$key] = ToUrlencode($temp[$key]); } else { $temp[$key] = urlencode($row); } } } else { $temp = $arr; } return $temp; } $arr = array('個人wod','個人wod','個人wod'); echo json_encode($arr); echo "\n"; echo toJson($arr); echo "\n";
結果:ajax