多层json数据编码转换

function tb_json_convert_encoding($m, $from, $to) {
    switch(gettype($m)) {
    case 'integer':
    case 'boolean':
    case 'float':
    case 'double':
    case 'NULL':
        return $m;
    case 'string':
        return mb_convert_encoding($m, $to, $from);
    case 'object':
        $vars = array_keys(get_object_vars($m));
        foreach($vars as $key) {
            $m->$key = tb_json_convert_encoding($m->$key, $from ,$to);
        }
        return $m;
    case 'array':
        foreach($m as $k => $v) {
            $m[tb_json_convert_encoding($k, $from, $to)] = tb_json_convert_encoding($v, $from, $to);
        }
        return $m;
    default:
    }
    return $m;
}

你可能感兴趣的:(笔试面试题)