codeigniter,result() 与 result_array() [英] codeigniter, result() vs. result_array()
问题描述
我同时使用 result()
和 result_array()
.
通常我喜欢将结果作为数组,这就是为什么我主要使用 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?
这是我在 codeigniter 查询中谈论的示例
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();
现在我也在我的通用模型中使用 result_array.
also right now i am using result_array in my generic model.
推荐答案
Result 有一个可选的 $type
参数,它决定返回什么类型的结果.默认情况下 ($type = "object"
),它返回一个对象 (result_object()
).可以设置为"array"
,然后返回一个结果数组,相当于调用result_array()
.第三个版本接受一个自定义类作为结果对象.
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.
来自 CodeIgniter 的代码:
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.
这篇关于codeigniter,result() 与 result_array()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!