php codeigniter,result() 与 result_array()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16506789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
codeigniter, result() vs. result_array()
提问by Sizzling Code
I use both result()
and result_array()
.
我同时使用result()
和result_array()
。
Usually i like to get my result as array thats why i use result_array() mostly..
通常我喜欢将结果作为数组,这就是为什么我主要使用 result_array() 的原因..
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
这是我在 codeigniter 查询中谈论的示例
$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_array 。
采纳答案by Whisperity
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.
Result 有一个可选$type
参数,它决定返回什么类型的结果。默认情况下 ( $type = "object"
),它返回一个对象 ( result_object()
)。它可以设置为"array"
,然后它将返回一个结果数组,相当于 caling result_array()
。第三个版本接受一个自定义类作为结果对象。
The code from CodeIgniter:
来自 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.
数组在技术上更快,但它们不是对象。这取决于你想在哪里使用结果。大多数情况下,数组就足够了。
回答by dsdsdsdsd
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 , ... )
...
)
回答by Emre Akay
result_array()
is faster,
result()
is easier
result_array()
更快,
result()
更容易
回答by Harat
result() returns Object type data. . . . result_array() returns Associative Array type data.
result() 返回对象类型数据。. . . result_array() 返回关联数组类型数据。
回答by Dan
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.
result()
是递归的,因为它返回一个 std 类对象,而它只返回result_array()
一个纯数组,因此result_array()
是关于性能的选择。虽然速度差异很小。
回答by Daniel Kmak
Returning pure array is slightly faster than returning an array of objects.
返回纯数组比返回对象数组稍快。
回答by Wahyu Artadianto
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
在我使用的遭遇这个问题result()
,并result_array()
在我的JSON
,如果使用result()
有没有问题,它的作品,但如果使用result_array()
我有错误"Trying to get property of non-object"
,所以我只是用这样的IM无法搜索到深的问题result()
,如果使用JSON
和使用result_array()
,如果不使用JSON