php - Manipulate JSON
710
I am developing a codeigniter application. I need the returned json data in this format.
{
options: [
{id:"1",text:"test"},
{id:"2",text:"kabita123"},
{id:"3",text:"purnima"},
{id:"4",text:"raju123"},
{id:"5",text:"saad123"},
{id:"12",text:"apf123"}
]
}
However, when returning the data, I only have it in this format
[{"id":"1","text":"test"},
{"id":"2","text":"kabita123"},
{"id":"3","text":"purnima"},
{"id":"4","text":"raju123"},
{"id":"5","text":"saad123"},
{"id":"12","text":"apf123"}]
Where am I doing it wrong ? The code I am using to generate the json data is
$users = $this->user_m->get_all();
$user_arr = [];
$i=0;
foreach ($users as $user) {
$user_arr[$i] = new stdClass();
$user_arr[$i]->id = $user->id;
$user_arr[$i]->text = $user->username;
$i++;
}
//add the header here
header('Content-Type: application/json');
echo json_encode( $user_arr );
Answer
Solution:
Change:
to: