如何使用 codeigniter 制作文件 .json?

How to make file .json with codeigniter?

这是我在控制器中的 json 编码:

public function loaddata(){
    $sql = $this->db->query('select * from e_alat_angkutan')->result();

    return json_encode($sql);

}

如何制作文件.json?请帮忙

在控制器__construct()方法中加载文件助手:

public function __construct()
{
    parent::__construct();
    $this->load->helper('file');
}

然后用第二个参数将其回显为 TRUE:

public function loaddata(){
    $sql = $this->db->query('select * from e_alat_angkutan')->result();

    echo json_encode($sql,TRUE);
}

作为这个答案的要点

//If the json is correct, you can then write the file and load the view

// $fp = fopen('./data.json', 'w');
// fwrite($fp, json_encode($sql));

// if ( ! write_file('./data.json', $arr))
// {
//     echo 'Unable to write the file';
// }
// else
// {
//     echo 'file written';
// }   

希望对您有所帮助!