38

I use both result() and result_array().

Usually I like to get my result as array that's why I use result_array() mostly.

But I want to know which is the faster approach that I should follow. Which one of them is more efficient to use in regards to performance?

Here is the example I am talking about in CodeIgniter queries:

$query = $this->db->get(); $result = $query->result_array(); 

or is this should be the better approach?

$query = $this->db->get(); $result = $query->result(); 

Also, right now, I am using result_array() in my generic model.

8 Answers 8

34

Result has an optional $type parameter which decides what type of result is returned. By default ($type = "object"), it returns an object (result_object()). It can be set to "array", then it will return an array of result, that being equivalent of caling result_array(). The third version accepts a custom class to use as a result object.

The code from CodeIgniter:

/** * Query result. Acts as a wrapper function for the following functions. * * @param string $type 'object', 'array' or a custom class name * @return array */ public function result($type = 'object') { if ($type === 'array') { return $this->result_array(); } elseif ($type === 'object') { return $this->result_object(); } else { return $this->custom_result_object($type); } } 

Arrays are technically faster, but they are not objects. It depends where do you want to use the result. Most of the time, arrays are sufficient.

Sign up to request clarification or add additional context in comments.

Comments

7

for the sake of reference:

// $query->result_object() === $query->result() // returns: Array ( [0] => stdClass Object ( [col_A] => val_1A , [col_B] => val_1B , ... ) [0] => stdClass Object ( [col_A] => val_2A , [col_B] => val_2B , ... ) ... ) // $query->result_array() !== $query->result() // returns: Array ( [0] => Array ( [col_A] => val_1A , [col_B] => val_1B , ... ) [1] => Array ( [col_A] => val_2A , [col_B] => val_2B , ... ) ... ) 

codeigniter docs for result(), and result_array()

Comments

4

result_array() is faster, result() is easier

Comments

3

result() returns Object type data. . . . result_array() returns Associative Array type data.

Comments

1

Returning pure array is slightly faster than returning an array of objects.

Comments

1

result() is recursive in that it returns an std class object where as result_array() just returns a pure array, so result_array() would be choice regarding performance. There is very little difference in speed though.

Comments

0

result_array() returns Associative Array type data. Returning pure array is slightly faster than returning an array of objects. result() is recursive in that it returns an std class object where as result_array() just returns a pure array, so result_array() would be choice regarding performance.

Comments

-1

in my experince the problem using result() and result_array() in my JSON if using result() there no problem its works but if using result_array() i got error "Trying to get property of non-object" so im not search into deep the problem so i just using result() if using JSON and using result_array() if not using JSON

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.