I have this code in my model when the controller requests to get all the messages for a certain user:
public function get_messages($user_id) {
$sql = "SELECT *
FROM messages
WHERE `to` = '$user_id'
ORDER BY id DESC";
$query = $this->db->query($sql);
foreach($query->result() as $row) {
$messages[] = array(
'id' => $row->id,
'to' => $row->to,
'from' => $row->from,
'message' => $row->message,
'star' => $row->star,
'timestamp' => $row->timestamp
);
}
return $messages;
}
Here’s the code in my controller:
$result = $this->inbox_m->get_messages($user['id']);
How can I return the result as a JSON object using PHP’s json_encode() function?
Normally I do something like this for simple returns:
json_encode(array('result' => true))
but all these arrays in arrays got me confused.
EDIT
Never mind guys, should have actually tried it before posting. This works just fine:
echo json_encode(array('result' => $result));
I will close the question in a day when my account allows me.
Closing the question by answering it myself. Like explained in the edit this works just fine: