PHP format JSON function sample code, JSON function sample code _php tutorial

Source: Internet
Author: User
Tags php json

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 ( ...)

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.