笨,结果()与result_array()(codeigniter, result() vs. re

2019-09-02 17:18发布

我使用这两种result()result_array()

平时我喜欢让我的结果作为数组这就是为什么我使用result_array()大多..

但我想知道哪个是更好的方法,我应该遵循,它们哪一个更有效的性能有关的使用?

下面是我在笨查询谈论例

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

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

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

还有现在我在我的通用模型使用result_array。

Answer 1:

结果有一个可选的$type ,其决定返回什么类型的结果的参数。 默认情况下( $type = "object" ),它返回一个对象( result_object() 它可以被设置为"array" ,那么它将返回结果的阵列,其被caling的等效result_array() 第三个版本接受一个自定义类作为结果对象使用。

从笨的代码:

/**
* 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);
    }
}

数组是技术上更快,但他们都没有对象。 这取决于你在哪里要使用的结果。 在大多数情况下,阵列就足够了。



Answer 2:

为了参考起见:

// $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 , ... )
        ... 
      ) 

笨文档对结果(),和result_array()



Answer 3:

result_array()速度更快, result()更容易



Answer 4:

结果()返回的对象类型的数据。 。 。 。 result_array()返回关联数组类型的数据。



Answer 5:

返回纯阵列比返回对象的数组稍快。



Answer 6:

result()是在它返回一个std类对象,其中递归result_array()只返回纯数组,所以result_array()将关于性能的选择。 有速度差别很小,虽然是。



Answer 7:

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



文章来源: codeigniter, result() vs. result_array()