欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php格式化json函數(shù)示例代碼

 更新時間:2016年05月12日 11:11:30   作者:懶人  
這篇文章主要介紹了php格式化json函數(shù),結(jié)合實例形式分析了php使用自定義函數(shù)實現(xiàn)json格式化的方法,涉及php數(shù)組轉(zhuǎn)json、流程控制及字符串操作等技巧,需要的朋友可以參考下

本文講述了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 = "&nbsp;&nbsp;&nbsp;";
  $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運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論