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

查看:303
本文介绍了codeigniter,result()vs. result_array()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 result() result_array()



通常我喜欢得到我的结果作为数组,这就是为什么我使用result_array()主要..



但我想知道哪个是更好的方法我应该遵循,
其中哪一个是更高效的使用性能?



这里是我正在谈论的例子我在codeigniter查询

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

或者这应该是更好的方法?

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

现在我在我的泛型模型中使用result_array。


<结果有一个可选的 $ type 参数,它决定返回什么类型的结果。默认情况下( $ type =object)返回一个对象( result_object())。它可以设置为array,那么它将返回一个结果数组,等价于计算 result_array()。第三个版本接受一个自定义类作为结果对象。



CodeIgniter的代码:

  / ** 
*查询结果。作为以下函数的包装函数。
*
* @param string $ type'object','array'或自定义类名
* @return数组
* /
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);
}
}

数组在技术上更快,但它们不是对象。这取决于你想使用的结果。大多数时候,数组就足够了。


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.

这篇关于codeigniter,result()vs. result_array()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆