php json數據保留原樣中文


php轉換json時, 其中的中文會轉換成Unicode, 要保持原來中文的意思

 /** 
     * 將數組里面帶有中文的字串保留以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";

結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM