【问题标题】:codeigniter, result() vs. result_array()codeigniter,result() 与 result_array()
【发布时间】:2013-05-06 14:03:52
【问题描述】:

我同时使用result()result_array()

通常我喜欢将结果作为数组获取,这就是我主要使用 result_array() 的原因..

但我想知道我应该遵循哪种更好的方法, 就性能而言,哪一个更有效?

这是我在 codeigniter 查询中讨论的示例

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

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

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

我现在也在我的通用模型中使用 result_array。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    Result 有一个可选的$type 参数,它决定返回什么类型的结果。默认情况下 ($type = "object"),它返回一个对象 (result_object())。可以设置为"array",然后返回一个结果数组,相当于调用result_array()。第三个版本接受一个自定义类作为结果对象。

    来自 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);
        }
    }
    

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

    【讨论】:

      【解决方案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 , ... )
              ... 
            ) 
      

      codeigniter docs for result(), and result_array()

      【讨论】:

        【解决方案3】:

        result_array() 更快, result() 更简单

        【讨论】:

          【解决方案4】:

          result() 返回对象类型数据。 . . . result_array() 返回关联数组类型数据。

          【讨论】:

            【解决方案5】:

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

            【讨论】:

              【解决方案6】:

              result() 是递归的,因为它返回一个 std 类对象,而 result_array() 只返回一个纯数组,所以 result_array() 将是性能方面的选择。不过速度差别很小。

              【讨论】:

                【解决方案7】:

                result_array() 返回关联数组类型数据。返回纯数组比返回对象数组稍快。 result() 是递归的,因为它返回一个 std 类对象,而 result_array() 只返回一个纯数组,所以 result_array() 将是性能方面的选择。

                【讨论】:

                  【解决方案8】:

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

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2018-12-21
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-11-09
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多