How to add title to json format output in codeigniter?

by dalton_moen , in category: PHP Frameworks , 7 months ago

How to add title to json format output in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , 7 months ago

@dalton_moen 

To add a title to JSON format output in CodeIgniter, you can follow these steps:

  1. In your Controller where you are generating JSON output, you can create an array and set the title in it along with other data. For example:
1
2
3
4
$data = array(
    'title' => 'My JSON Data',
    'data' => $jsonData // Your JSON data
);


  1. Next, you can encode this array into JSON format using the json_encode() function:
1
$jsonOutput = json_encode($data);


  1. Finally, you can send this JSON output with the title using the set_content_type() and print() or echo functions:
1
2
3
$this->output
    ->set_content_type('application/json')
    ->set_output($jsonOutput);


With these steps, you can add a title to your JSON format output in CodeIgniter.