0

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?

3
  • if you want response in object then return $query->result(); only Commented Mar 30, 2017 at 12:56
  • Why don't you just call result() instead of result_array()? Also, if you are just encoding it as JSON anyway, why does it matter if you use result() or result_array()? What problem are you trying to solve here exactly? What output are you getting compared to the output you want? Commented Mar 30, 2017 at 14:28
  • I was solved the problem, result() or result_array(), json outputs are same like above, Commented Mar 30, 2017 at 15:39

1 Answer 1

3

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.