'; } $k = strlen($space)?strlen($space):1; for ($i=0; $i<=$len; $i++) { $char = substr($json, $i, 1); if($char == '}' || $char == ']') { $cnt --; $out .= $nl . str_pad('', ($tab * $cnt * $k), $space); } else if($char == '{' || $char == '[') { $cnt ++; } $out .= $char; if($char == ',' || $char == '{' || $char == '[') { $out .= $nl . str_pad('', ($tab * $cnt * $k), $space); } if($char == ':') { $out .= ' '; } } return $out; } $arr = array('name'=>'John', 'age'=>19, 'address'=>array(array('city'=>'Nashville', 'country'=>'US'))); $json_string = json_encode($arr); echo json_pretty($json_string, true); // Pass true as the second parameter to get the output in HTML ?>