PHP format JSON function sample code, JSON function sample code
This article describes the sample code for PHP formatted JSON functions. Share to everyone for your reference, as follows:
<?php$arr = Array ("ret" =>0, "Data" =>array (' a ' = = 1, ' b ' = ' 2 ', ' c ' = 3, ' d ' = = 4, ' e ' = 5) "$jso n = json_encode ($arr);/*** Formats A JSON string for pretty printing** @param string $json The JSON-make pretty* @param BOOL $html Insert nonbreaking spaces and
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 = "
"; } 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}}**/?>
In addition, the site also provides the following format and conversion tools to facilitate the use of:
PHP Code online format Beautification tool:
Http://tools.jb51.net/code/phpformat
Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson
JavaScript code beautification/compression/formatting/encryption Tools:
Http://tools.jb51.net/code/jscompress
Online XML format/compression tool:
Http://tools.jb51.net/code/xmlformat
More about PHP related content readers can view the topic: "PHP JSON Format data Operation skills Summary", "PHP file Operation Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", " PHP Operations Office Documentation Tips Summary (including word,excel,access,ppt), PHP date and Time usage summary, PHP Object-oriented Programming primer tutorial, PHP string Usage Summary, php+ MySQL database operation Getting Started tutorial and PHP Common database operation Skills Summary
I hope this article is helpful to you in PHP programming.
http://www.bkjia.com/PHPjc/1125870.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125870.html techarticle PHP format JSON function sample code, JSON function sample code This article describes the PHP format JSON function sample code. Share to everyone for your reference, specifically as follows: Php$arr = Array ( ...)