function json_encode_utf_normal($value) { if (is_int($value)) { return (string) $value; } elseif (is_string($value)) { $value = str_replace(array('\\', '/', '"', "\r", "\n", "\\b", "\f", "\t"), array('\\\\', '\\/', '\\"', '\\r', '\\n', '\\b', '\\f', '\\t'), $value); $convmap = array(0x80, 0xffff, 0, 0xffff); $result = ""; for ($i = strlen($value) - 1; $i >= 0; $i--) { $mb_char = substr($value, $i, 1); $result = $mb_char . $result; } return '"' . $result . '"'; } elseif (is_float($value)) { return str_replace(",", ".", $value); } elseif (is_null($value)) { return 'null'; } elseif (is_bool($value)) { return $value ? 'true' : 'false'; } elseif (is_array($value)) { $with_keys = false; $n = count($value); for ($i = 0, reset($value); $i < $n; $i++, next($value)) { if (key($value) !== $i) { $with_keys = true; break; } } } elseif (is_object($value)) { $with_keys = true; } else { return ''; } $result = array(); if ($with_keys) { foreach ($value as $key => $v) { $result[] = json_encode_utf_normal((string) $key) . ':' . json_encode_utf_normal($v); } return '{' . implode(',', $result) . '}'; } else { foreach ($value as $key => $v) { $result[] = json_encode_utf_normal($v); } return '[' . implode(',', $result) . ']'; } }
public static function jsonEncode($arr) { return is_array($arr) || is_object($arr) ? json_encode_utf_normal($arr) : json_encode_utf_normal(array()); }