Pretty-Printing JSON with PHP

后端 未结 24 1877
一向
一向 2020-11-22 02:03

I\'m building a PHP script that feeds JSON data to another script. My script builds data into a large associative array, and then outputs the data using json_encode

24条回答
  •  攒了一身酷
    2020-11-22 02:51

    If you are working with MVC

    try doing this in your controller

    public function getLatestUsers() {
        header('Content-Type: application/json');
        echo $this->model->getLatestUsers(); // this returns json_encode($somedata, JSON_PRETTY_PRINT)
    }
    

    then if you call /getLatestUsers you will get a pretty JSON output ;)

提交回复
热议问题