PHP Custom Function format JSON data sample _php Tips

Source: Internet
Author: User
Tags getting started with php php database php json

The examples in this article describe how PHP custom functions format JSON data. Share to everyone for your reference, specific as follows:

<?php/** * Formats A JSON string for pretty printing * * @param string $json The JSON-make pretty * @param bool $html Insert nonbreaking spaces and <br/>s for tabs and linebreaks * @return string The prettified o Utput */$arr = array ("ret" =>0, "Data" =>array (' a ' => 1, ' B ' => "cloud-Habitat community", ' C ' => 3, ' d ' => 4, ' E ' => 5
));
$json = Json_encode ($arr);
    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": "\u811a\u672c\u4e4b\u5bb6", "C": 3, "D": 4, "E": 5}} **/

 ?>

PS: Here again for you to recommend a few more practical JSON online tools for your reference to use:

Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json

JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat

Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson

JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat

C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json

For more information about PHP interested readers can view the site topics: "PHP JSON format data Operation tips Summary", "PHP for XML file Operation skills Summary", "PHP basic Grammar Introductory Course", "PHP Array" Operation Techniques Encyclopedia, "PHP string ( String) Usage summary, "Getting Started with Php+mysql database operations" and "Summary of common PHP database Operations Tips"

I hope this article will help you with the PHP program design.

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.