json_encode替代函数
来源:互联网 发布:nginx 只允许访问目录 编辑:程序博客网 时间:2024/05/16 03:55
<?php
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 be associative. If the array is empty or if it
// has sequential whole number keys starting with 0, it's not associative
// so we can go ahead and convert it as an array.
if (empty ($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';
}
}
}
$arr=array('name'=>'CSDN论坛','degree'=>'numberone');
echo jsonEncode($arr);
?>
- json_encode替代函数
- json_encode替代函数
- json_encode 函数
- PHP json_encode() 函数介绍
- PHP json_encode() 函数介绍
- PHP json_encode() 函数介绍
- PHP json_encode() 函数介绍
- PHP json_encode() 函数介绍
- PHP json_encode() 函数介绍
- PHP json_encode() 函数
- PHP json_encode() 函数介绍
- 【json】json_encode() 函数介绍
- PHP json_encode() 函数介绍
- PHP json_encode() 函数介绍
- PHP json_encode() 函数介绍
- PHP json_encode() 函数介绍
- PHP json_encode() 函数介绍
- PHP json_encode() 函数介绍
- 创业笔记(八)
- Direct3D 简介
- Beaglebone 没有操作系统Demo程序运行
- jquery - 日历插件(自定义模板的日历插件)
- sqlite注意
- json_encode替代函数
- 定时器 Quartz+spring实例应用
- libmemcached安装undefined reference to `__sync_fetch_and_add_4'
- C#引用c++DLL结构体数组注意事项(数据发送与接收时)
- jquery - 公历转农历方法
- mailing.freebsd.current
- 牛顿迭代法和最小二乘法直线拟合代码
- undefined references: __sync_fetch_and_add_4 and __sync_val_compare_and_swap_4
- apche server 中配置别名【也可以设定网络路径】