[php] codeigniter, result() vs. result_array()

I use both result() and result_array().

Usually i like to get my result as array thats why i use result_array() mostly..

But i want to know which is the better 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.

This question is related to php codeigniter

The answer is


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.


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


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


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


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()


result_array() is faster, result() is easier


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.