Report abuse
function php_json_encode($obj) {
switch (gettype($obj)) {
case 'object':
$obj = get_object_vars($obj);
case 'array':
if (array_is_associative($obj)) {
$arr_out = array();
foreach ($obj as $key=>$val) {
$arr_out[] = '"' . $key . '":' . php_json_encode($val);
}
return '{' . implode(',', $arr_out) . '}';
} else {
$arr_out = array();
$ct = count($obj);
for ($j = 0; $j < $ct; $j++) {
$arr_out[] = php_json_encode($obj[$j]);
}
return '[' . implode(',', $arr_out) . ']';
}
break;
case 'NULL':
return 'null';
break;
case 'boolean':
return ($obj)? 'true' : 'false';
break;
case 'integer':
case 'double':
return $obj;
break;
case 'string':
default:
$obj = str_replace(array('\\','/','"',), array('\\\\','\/','\"'), $obj);
return '"' . $obj . '"';
break;
}
}