note 98040 added to function.json-encode

From: Date: Sat, 22 May 2010 16:09:44 +0000
Subject: note 98040 added to function.json-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-169514@lists.php.net to get a copy of this message
The function json_encode does not return the JSON code in 'human-readable' formats. The function below will format any JSON code given to it. <?php function json_format($input){ $tab = 0; $out = ""; $tabs = ""; for($i=0;$i<strlen($input);$i++){ $c = $input[$i]; $tabs = ""; for($t=1;$t<=$tab;$t++) $tabs .= "\t"; switch($c){ case "{": $tab++; $tabs .= "\t"; $out .= "{\n".$tabs; break; case "}": $tab--; $tabs[strlen($tabs)-1] = ""; $out .= "\n".$tabs."}"; break; case "[": $tab++; $tabs .= "\t"; $out .= "[\n".$tabs; break; case "]": $tab--; $tabs[strlen($tabs)-1] = ""; $out .= "\n".$tabs."]"; break; case ",": $out .= ",\n".$tabs; break; default: $out .= $c; break; } } $out = str_replace("\x00", "", $out); return $out; } ?> Example: <?php $a = array("Name"=>"Angelo","Age"=>16, "Interests"=>array("Games","Television","Holidays")); echo json_format(json_encode($a)); ?> This will output: { "Name":"Angelo", "Age":16, "Interests":[ "Games", "Television", "Holidays" ] } ---- Server IP: 213.249.64.162 Probable Submitter: 80.101.80.110 ---- Manual Page -- http://www.php.net/manual/en/function.json-encode.php Edit -- https://master.php.net/note/edit/98040 Del: integrated -- https://master.php.net/note/delete/98040/integrated Del: useless -- https://master.php.net/note/delete/98040/useless Del: bad code -- https://master.php.net/note/delete/98040/bad+code Del: spam -- https://master.php.net/note/delete/98040/spam Del: non-english -- https://master.php.net/note/delete/98040/non-english Del: in docs -- https://master.php.net/note/delete/98040/in+docs Del: other reasons-- https://master.php.net/note/delete/98040 Reject -- https://master.php.net/note/reject/98040 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#169514) next »