Tools, FAQ, Tutorials:
json_encode() - JSON_PRETTY_PRINT Option
How to use the JSON_PRETTY_PRINT option in the json_encode() call to generate a pretty looking JSON text string?
✍: FYIcenter.com
If you want a pretty looking JSON text string,
you can call json_encode() with the JSON_PRETTY_PRINT option.
It will add line breaks and insert 4 space characters to indent
level of sub structures in the output.
Here is a PHP example that shows you how to use the JSON_PRETTY_PRINT when calling json_decode():
<?php
"dev.fyicenter.com",
"1"=>array("0"=>"PHP","1"=>"JSON")
);
print("\nInput: ");
var_dump($var);
print("Output: ".json_encode($var,JSON_PRETTY_PRINT)."\n");
$var = array(
"site"=>"dev.fyicenter.com",
"topics"=>array("PHP"=>"Y","JSON"=>"Y")
);
print("\nInput: ");
var_dump($var);
print("Output: ".json_encode($var,JSON_PRETTY_PRINT)."\n");
?>
If you run the above PHP code through the PHP engine, you get the following output:
>\fyicenter\php\php.exe json_encode_pretty.php
Input: array(2) {
[0]=>
string(17) "dev.fyicenter.com"
[1]=>
array(2) {
[0]=>
string(3) "PHP"
[1]=>
string(4) "JSON"
}
}
Output: [
"dev.fyicenter.com",
[
"PHP",
"JSON"
]
]
Input: array(2) {
["site"]=>
string(17) "dev.fyicenter.com"
["topics"]=>
array(2) {
["PHP"]=>
string(1) "Y"
["JSON"]=>
string(1) "Y"
}
}
Output: {
"site": "dev.fyicenter.com",
"topics": {
"PHP": "Y",
"JSON": "Y"
}
}
2023-08-17, ∼2277🔥, 0💬
Popular Posts:
How to convert a JSON text string to an XML document with PHP language? Currently, there is no built...
How to read Atom validation errors at w3.org? If your Atom feed has errors, the Atom validator at w3...
Where can I download the EPUB 2.0 sample book "The Metamorphosis" by Franz Kafka? You can following ...
Can Multiple Paragraphs Be Included in a List Item? Yes. You can include multiple paragraphs in a si...
How To Loop through an Array without Using "foreach" in PHP? PHP offers the following functions to a...