note 71052 added to function.json-decode
| From: | fudriot at omic dot ch | Date: | Thu, 09 Nov 2006 18:11:09 +0000 |
| Subject: | note 71052 added to function.json-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119566@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: 194.153.94.10
Probable Submitter: 83.180.94.61
----
Manual Page -- http://www.php.net/manual/en/function.json-decode.php
Edit -- https://master.php.net/note/edit/71052
Del: integrated -- https://master.php.net/note/delete/71052/integrated
Del: useless -- https://master.php.net/note/delete/71052/useless
Del: bad code -- https://master.php.net/note/delete/71052/bad+code
Del: spam -- https://master.php.net/note/delete/71052/spam
Del: non-english -- https://master.php.net/note/delete/71052/non-english
Del: in docs -- https://master.php.net/note/delete/71052/in+docs
Del: other reasons-- https://master.php.net/note/delete/71052
Reject -- https://master.php.net/note/reject/71052
Search -- https://master.php.net/manage/user-notes.php