简体   繁体   中英

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.

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.

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()更快, result()更容易

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

返回纯数组比返回对象数组稍快。

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.

在我的经验中,在我的JSON使用result()result_array()的问题如果使用result()没有问题,但如果使用result_array()我得到错误"Trying to get property of non-object"所以我没有搜索深入问题所以我只使用result()如果使用JSON和使用result_array()如果不使用JSON

result() returns Object type data

result_array() returns Array type data.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM