Json_encode function to print php json indent
When encoding an associative array to json using the json_encode function in php, it is output as a single line without indentation.
<?php
$arr['user'] = "kim";
$arr['age'] ="15";
$arr['phone_number'] = "1111-1111";
echo json_encode($arr);
?>

If the JSON is long, you can use a site that sorts JSON because it is very hard to see.
jsonformatter.curiousconcept.com

This json encoding process is provided in php's json_encode by default.
Just give JSON_PRETTY_PRINT as the second argument of json_encode.
This argument has been supported since php 5.4.
<?php
$arr['user'] = "kim";
$arr['age'] ="15";
$arr['phone_number'] = "1111-1111";
echo json_encode($arr,JSON_PRETTY_PRINT);
?>
www.php.net
The output is as follows.

Comments
Post a Comment