note 71378 added to function.json-encode
| From: | fudriot at omic dot ch | Date: | Sat, 25 Nov 2006 09:27:52 +0000 |
| Subject: | note 71378 added to function.json-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120068@lists.php.net to get a copy of this message | ||
One of the purpose of JSON is to be easy for humans to read and write. But the output of
json_encode() doen't indent very nicely... a "flat" string !
This function might help to have a nice identation :)
<?php
echo indentJson(json_encode($anArray));
function indentJson($str){
$strOut = '';
$identPos = 0;
for($loop = 0;$loop<= strlen($str) ;$loop++){
$_char = substr($str,$loop,1);
//part 1
if($_char == '}' || $_char == ']'){
$strOut .= chr(13);
$identPos --;
for($ident = 0;$ident < $identPos;$ident++){
$strOut .= chr(9);
}
}
//part 2
$strOut .= $_char;
//part 3
if($_char == ',' || $_char == '{' || $_char == '['){
$strOut .= chr(13);
if($_char == '{' || $_char == '[')
$identPos ++;
for($ident = 0;$ident < $identPos;$ident++){
$strOut .= chr(9);
}
}
}
return $strOut;
?>
----
Server IP: 129.16.214.50
Probable Submitter: 83.181.109.148
----
Manual Page -- http://www.php.net/manual/en/function.json-encode.php
Edit -- https://master.php.net/note/edit/71378
Del: integrated -- https://master.php.net/note/delete/71378/integrated
Del: useless -- https://master.php.net/note/delete/71378/useless
Del: bad code -- https://master.php.net/note/delete/71378/bad+code
Del: spam -- https://master.php.net/note/delete/71378/spam
Del: non-english -- https://master.php.net/note/delete/71378/non-english
Del: in docs -- https://master.php.net/note/delete/71378/in+docs
Del: other reasons-- https://master.php.net/note/delete/71378
Reject -- https://master.php.net/note/reject/71378
Search -- https://master.php.net/manage/user-notes.php