作者:劲舞团之舞之精灵_559 | 来源:互联网 | 2013-05-13 10:10
functionarrayToJson($array){arrayForRecursive($array,'urlencode',true);$json=json_encode($array);$json=urldecode($json);return$json;}
function arrayToJson($array) { arrayForRecursive($array, 'urlencode', true); $json = json_encode($array); $json = urldecode($json); return $json; }
function arrayForRecursive(&$array, $function, $applytokeys_also=false) { static $recursive_counter = 0; if (++$recursive_counter>1000) die('数组层次太深!'); foreach ($array as $key=>$value) { if (is_array($value)) arrayForRecursive($array[$key], $function, $applytokeys_also); else $array[$key] = $function(repalceSpecialSign($value)); if ($applytokeysalso&&isstring($key)) { $new_key = $function($key); if ($new_key!=$key) { $array[$new_key] = $array[$key]; unset($array[$key]); } } } $recursive_counter--; }
function struct2Array($item) { if (!is_string($item)) { $item = (array)$item; foreach ($item as $key=>$val) { $item[$key] = Struct2Array($val); } } return $item; }
/** * repalceSpecialSign,替换特殊符号. * @param str string 一个字符串 * @return str */ function repalceSpecialSign($string) { $string = preg_replace("/\s/","",$string); $string = str_replace("\","\\",$string); $string = str_replace("\'","\\'",$string); $string = str_replace("\"","\\"",$string); $string = strreplace(PHPEOL,'',$string); $string = str_replace("\n","",$string); $string = str_replace("\r","",$string); return $string; }
调用arraytojson就行了。
http://www.pooy.net/original-php-json-function-json-format.html