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);
  
 ?>

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


 
JSON Formatter & Validator
Format and validate JSON data so that it can easily be read by human beings.
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);
  
 ?>

 
PHP: json_encode-Manual
...
www.php.net
The output is as follows.

Comments

Popular posts from this blog

Enabling Com Port on Hyper-V 2nd Generation VM / Windbg

DLL Injection using CreateRemoteThread in Windows 10

Using Overlapped I / O in DeviceIoControl