cleargo 2017-05-08 16:25:26 3753次浏览 0条评论 2 1 0
public function printJson($array)
{
    var_export($this->jsonFormat(json_encode($array, 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;
}
觉得很赞
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册