php JSON 中文亂碼解決方案

<?php
// 將數組轉換成Json格式,中文需要進行URL編碼處理
function Array2Json($array) {
    arrayRecursive($array, 'urlencode', true);
    $json = json_encode($array);
    $json = urldecode($json);
    // ext需要不帶引號的bool類型
    $json = str_replace("/"false/"","false",$json);
    $json = str_replace("/"true/"","true",$json);
    return $json;
}

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--;
}
?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章