Report abuse

/**
 * input an object, returns a json-ized string of said object
 * 
 * This function doesn't support special UTF-8 characters.
 * 
 * @param mixed $obj The array or object to encode
 * @return string JSON formatted output
 * @author Chip Harlan (Original Creator)
 * @author Jay Williams (Added additional variable types)
 * @link http://www.post-hipster.com/2008/02/15/elegant-little-php-json-encoder/
 */
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;
    }

}