1

What i am trying to do is convert result_array() given by my query and directly convert it into JSON object , which can further be set in controller like this:

$this->response($jsonobject, REST_Controller:: HTTP_OK);

my attempt by studying several SO questions :

$this->response(json_encode($result_array), REST_Controller:: HTTP_OK);

but my attempt is wrong i think because it return json itself in form of string , how to achieve this then?

Example:

$pakistan = array('status' => 'OK','message' => 'yes i am ok');
// convert pakistan to something like
$pakistan = [ 'status' => 'OK','message' => 'yes i am ok' ];
6
  • post an example Commented Sep 24, 2016 at 9:18
  • just posted example @PathikVejani Commented Sep 24, 2016 at 9:23
  • What framework are you using, json encode gives a string representation of an object/array. Have you tried just passing result_array to response? Commented Sep 24, 2016 at 9:28
  • nop , didn't tried it yet , but i think that's bad idea , it is expecting object as response not array Commented Sep 24, 2016 at 9:30
  • @BenCummins just tried your theory , and got 500: Internal Server Error Commented Sep 24, 2016 at 9:33

1 Answer 1

3

Try to return the query result not result_array

model function

function getRecords(){
   $sql = 'SELECT * FROM table';
   $query = $this->db->query($sql);
   return $query->result();
}

Convert to json

$object = getRecords();
$json_obj = json_encode($object);
Sign up to request clarification or add additional context in comments.

4 Comments

should i pass $object or $json_obj to response ?
Pass the $json_obj to response
I don't know why some one voted my answer down, did I some thing wrong?
Thanks for hint , solution was using result() instead of result_array() ,

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.