Latief Anwar
Latief Anwar

Reputation: 1868

How to convert result_array() to result() object in codeigniter

I need JSON output like this,

{"restaurant":[{"id":"89","owner_id":"2161","name":"Bakso ",...}{"id":"90","owner_id":"3400","name":"Soto"}]}

And I do using this query and work perfectly:

$latest_rest = $this->Restaurants_model->getLatestRestaurants($limit)->result();
$jsonObject = array('restaurant'=>$latest_rest);

but i have query with result_array(), because I must filtering this data through function that return result_array();

how do I convert result_array() to result() in CodeIgniter?

Upvotes: 0

Views: 1515

Answers (1)

Saroj Shrestha
Saroj Shrestha

Reputation: 2875

If it is mandatory to get result in array in beginning then to get json data you can first encode data into json and later can decode as:

$object = json_decode(json_encode($array));

And, if it is not necessary in beginning then you can get result in object directly as:

$this->db->select('*')->from..........;
$query = $this->db->get();
return $query->result(); // instead of result_array();

Upvotes: 3

Related Questions