cleargo 2017-05-14 23:14:49 2290次浏览 0条回复 0 1 0
public function printJson($array)
{
    var_export($this->jsonFormat(json_encode(json_decode($array, true), JSON_UNESCAPED_UNICODE)));
}

function jsonFormat($data, $indent = null)
{
    // 缩进处理
    $ret = '';
    $pos = 0;
    $length = strlen($data);
    $indent = isset($indent) ? $indent : '    ';
    $newline = "\n";
    $prevChar = '';
    $outofquotes = true;
    for ($i = 0; $i <= $length; $i++) {
        $char = substr($data, $i, 1);
        if ($char == '"' && $prevChar != '\\') {
            $outofquotes = !$outofquotes;
        } elseif (($char == '}' || $char == ']') && $outofquotes) {
            $ret .= $newline;
            $pos--;
            for ($j = 0; $j < $pos; $j++) {
                $ret .= $indent;
            }
        }
        $ret .= $char;
        if (($char == ',' || $char == '{' || $char == '[') && $outofquotes) {
            $ret .= $newline;
            if ($char == '{' || $char == '[') {
                $pos++;
            }
            for ($j = 0; $j < $pos; $j++) {
                $ret .= $indent;
            }
        }
        $prevChar = $char;
    }
    return $ret;
}
觉得很赞
    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册