- /**
- * Alternative functions for Json_encode
- * Edit bbs.it-home.org
- */
- function Jsonencode ($var) {
- if (function_exists (' Json_encode ')) {
- Return Json_encode ($var);
- } else {
- Switch (GetType ($var)) {
- Case ' Boolean ':
- Return $var? ' True ': ' false '; lowercase necessary!
- Case ' integer ':
- Case ' Double ':
- return $var;
- Case ' resource ':
- Case ' string ':
- Return ' ". Str_replace (Array ("\ r", "\ n", "<", ">", "&"),
- Array (' \ r ', ' \ n ', ' \x3c ', ' \x3e ', ' \x26 '),
- Addslashes ($var)). ' ";
- Case ' array ':
- Arrays in JSON can ' t is associative. If the array is empty or if it
- have sequential whole number keys starting with 0, it's not associative
- So we can go ahead and convert it as an array.
- if (Emptyempty ($var) | | Array_keys ($var) = = = Range (0, sizeof ($var)-1)) {
- $output = Array ();
- foreach ($var as $v) {
- $output [] = Jsonencode ($v);
- }
- Return ' ['. Implode (', ', $output). '] ';
- }
- Otherwise, fall through to convert the array as an object.
- Case ' object ':
- $output = Array ();
- foreach ($var as $k = = $v) {
- $output [] = Jsonencode (Strval ($k)). ': '. Jsonencode ($v);
- }
- Return ' {'. Implode (', ', $output). '} ';
- Default
- return ' null ';
- }
- }
- }
- echo jsonencode (Array (' first ' = ' testing ', ' second ' = ' Tangjili '));
- ?>
Copy CodeThis can also be the code below.
- function Php_json_encode ($data) {
- if (Is_array ($data) | | is_object ($DATA)) {
- $islist = Is_array ($data) && (Emptyempty ($data) | | Array_keys ($data) = = = Range (0,count ($data)-1));
- if ($islist) $json = ' ['. Implode (', ', Array_map (' Php_json_encode ', $data)). ']';
- else {
- $items = Array ();
- foreach ($data as $key = = $value) $items [] = Php_json_encode ("$key"). ':' . Php_json_encode ($value);
- $json = ' {'. Implode (', ', $items). '}';
- }
- } elseif (Is_string ($data)) {
- $string = ' "'. Addcslashes ($data, "\\\" \n\r\t/". Chr (8). chr (12)). '"';
- $json = ";
- $len = strlen ($string);
- for ($i = 0; $i < $len; $i + +) {
- $char = $string [$i];
- $c 1 = ord ($char);
- if ($c 1 <128) {$json. = ($c 1 >)? $char: sprintf ("\\u%04x", $c 1); continue;}
- $c 2 = Ord ($string [+ + $i]);
- if (($c 1 &) = = = 0) {$json. = sprintf ("\\u%04x", ($c 1-192) * + $c 2-128); continue;}
- $c 3 = Ord ($string [+ + $i]);
- if (($c 1 &) = = = 0) {$json. = sprintf ("\\u%04x", (($c 1-224) <<12) + (($c 2-128) << 6) + ($c 3-128) ); Continue }
- $c 4 = Ord ($string [+ + $i]);
- if (($c 1 & 8) = = = 0) {
- $u = (($c 1 &) << 2) + (($c 2>>4) & 3)-1;
- $w 1 = (54<<10) + ($u <<6) + (($c 2 &) << 2) + (($c 3>>4) & 3);
- $w 2 = (55<<10) + (($c 3 &) <<6) + ($c 4-128);
- $json. = sprintf ("\\u%04x\\u%04x", $w 1, $w 2);
- }
- }
- }
- else $json = Strtolower (Var_export ($data, true));
- return $json;
- }
- echo php_json_encode (Array (' first ' = ' testing '));
- ?>
Copy CodeChinese words, can be combined with the following function to use.
- Function arrayrecursive (& $array, $function, $apply _to_keys_also = False)
- {
- 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]); } }
- }
- }
- ?>
Copy CodeInvocation Example:
- function JSON ($array) {
- Arrayrecursive ($array, ' UrlEncode ', true);
- $json = Jsonencode ($array); or $json = Php_json_encode ($array);
- Return UrlDecode ($json);
- }
- Echo JSON (' first ' = ' testing ', ' second ' = ' Chinese ');
- ?>
Copy Code |