echo in json format php

PHP
<?php
...
$json_string = json_encode($data, JSON_PRETTY_PRINT);
Source

Also in PHP: