php格式化json函數(shù)示例代碼
本文講述了php格式化json函數(shù)的示例代碼。分享給大家供大家參考,具體如下:
<?php $arr = array("ret"=>0,"data"=>array('a' => 1, 'b' => '2', 'c' => 3, 'd' => 4, 'e' => 5)); $json = json_encode($arr); /** * Formats a JSON string for pretty printing * * @param string $json The JSON to make pretty * @param bool $html Insert nonbreaking spaces and <br />s for tabs and linebreaks * @return string The prettified output */ function _format_json($json, $html = false) { $tabcount = 0; $result = ''; $inquote = false; $ignorenext = false; if ($html) { $tab = " "; $newline = "<br/>"; } else { $tab = "\t"; $newline = "\n"; } for($i = 0; $i < strlen($json); $i++) { $char = $json[$i]; if ($ignorenext) { $result .= $char; $ignorenext = false; } else { switch($char) { case '{': $tabcount++; $result .= $char . $newline . str_repeat($tab, $tabcount); break; case '}': $tabcount--; $result = trim($result) . $newline . str_repeat($tab, $tabcount) . $char; break; case ',': $result .= $char . $newline . str_repeat($tab, $tabcount); break; case '"': $inquote = !$inquote; $result .= $char; break; case '\\': if ($inquote) $ignorenext = true; $result .= $char; break; default: $result .= $char; } } } return $result; } echo _format_json($json); /* { "ret": 0, "data": { "a": 1, "b": 2, "c": 3, "d": 4, "e": 5 } } **/ ?>
另外,本站還提供了如下格式化與轉(zhuǎn)換工具方便大家使用:
php代碼在線格式化美化工具:
http://tools.jb51.net/code/phpformat
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
JavaScript代碼美化/壓縮/格式化/加密工具:
http://tools.jb51.net/code/jscompress
在線XML格式化/壓縮工具:
http://tools.jb51.net/code/xmlformat
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP中json格式數(shù)據(jù)操作技巧匯總》、《php文件操作總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php返回json數(shù)據(jù)函數(shù)實(shí)例
- PHP使用json_encode函數(shù)時(shí)不轉(zhuǎn)義中文的解決方法
- php json_encode()函數(shù)返回json數(shù)據(jù)實(shí)例代碼
- PHP json_decode函數(shù)詳細(xì)解析
- Javascript生成json的函數(shù)代碼(可以用php的json_decode解碼)
- PHP json_encode() 函數(shù)詳解及中文亂碼問題
- 老版本PHP轉(zhuǎn)義Json里的特殊字符的函數(shù)
- php5.2以下版本無json_decode函數(shù)的解決方法
- PHP自定義函數(shù)格式化json數(shù)據(jù)示例
- java解析php函數(shù)json_encode unicode 編碼問題
- 關(guān)于php中的json_encode()和json_decode()函數(shù)的一些說明
- php json相關(guān)函數(shù)用法示例
相關(guān)文章
PHP7安裝Redis擴(kuò)展教程【Linux與Windows平臺(tái)】
這篇文章主要介紹了PHP7安裝Redis擴(kuò)展的方法,簡(jiǎn)單分析了Linux與Windows平臺(tái)Redis擴(kuò)展的安裝方法,并提供了一個(gè)實(shí)例進(jìn)一步分析了php操作Redis的相關(guān)技巧,需要的朋友可以參考下2016-09-09PHP5.0 TIDY_PARSE_FILE緩沖區(qū)溢出漏洞的解決方案
這篇文章主要給大家介紹了關(guān)于PHP5.0 TIDY_PARSE_FILE緩沖區(qū)溢出漏洞的解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10php正則取img標(biāo)記中任意屬性(正則替換去掉或改變圖片img標(biāo)記中的任意屬性)
因有一項(xiàng)目新聞發(fā)布系統(tǒng),數(shù)據(jù)庫內(nèi)容字段中存儲(chǔ)的是原圖的路徑(當(dāng)然還有其他文字內(nèi)容啦,內(nèi)容里插圖時(shí),存的是圖片路徑),但前臺(tái)想使用縮略圖,琢磨1小時(shí)余,得到以下結(jié)果,可解決問題2013-08-08php出現(xiàn)web系統(tǒng)多域名登錄失敗的解決方法
這篇文章主要介紹了php出現(xiàn)web系統(tǒng)多域名登錄失敗的解決方法,針對(duì)同一頂級(jí)域名下的各子站出現(xiàn)登錄失敗的情況非常有用,需要的朋友可以參考下2014-09-09php通過sort()函數(shù)給數(shù)組排序的方法
這篇文章主要介紹了php通過sort()函數(shù)給數(shù)組排序的方法,實(shí)例分析了php中sort()函數(shù)的功能及相關(guān)使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03php中http與https跨域共享session的解決方法
這篇文章主要介紹了http與https跨域共享session的解決方法,需要的朋友可以參考下2014-12-12