JSON data you should have encountered, Json_encode () and Json_decode () php5.0 is added after the built-in function, if the lower version to use, need to expand, many times we do not have the right to change the configuration of the server, we can only through the custom function to implement these two functions, in fact, all the system built-in functions, basically we can be defined by ourselves.
if(!function_exists(' Json_encode ')) {functionJson_encode ($array=Array()) {if(!Is_array($array))return NULL;$json= "";$i= 1;$comma= ",";$count=Count($array);foreach($array as $k=$v){if($i==$count)$comma= "";if(!Is_array($v)){$v=addslashes($v);$json. = ' "'.$k.‘":"‘.$v.‘"‘.$comma;}Else{$json. = ' "'.$k. ' ": '. Json_encode ($v).$comma;}$i++;}$json= ' {'.$json.‘}‘;return $json;}}if(!function_exists(' Json_decode ')) {functionJson_decode ($json,$assoc=true) {$comment=false;$out= ' $x = ';$json=Preg_replace('/:([^ '}]+?] ([,|}]) /I ', ': "\1″\2′, $json); for ($i = 0; $i <strlen ($json); $i + +) {if (! $comment) {if ($json [$i] = =' {') | | ($json [$i] = = ' [') {$out. =‘Array(‘;} ElseIf ($json [$i] = ='} ') | | ($json [$i] = = '] ') {$out. =‘)‘;} ElseIf ($json [$i] = =‘:‘) {$out. =' = ';} ElseIf ($json [$i] = =‘,‘) {$out. =‘,‘;} ElseIf ($json [$i] = =‘"') {$out. = ' "';}} else $out. = $json [$i] = =' $ '? ' \$ ': $json [$i];if ($json [$i] = ="' &&$json[($i-1)]! = ' \ \ ')$comment= !$comment;} Eval ($out. ‘;‘); Return$x;}}
PHP custom Json_encode () and Json_decode () functions