php – codeigniter,result()与result_array()

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – codeigniter,result()与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.

解决方法

Result有一个可选的$type参数,用于决定返回的结果类型.认情况下($type =“object”),它返回一个对象(result_object()).它可以设置为“array”,然后它将返回一个结果数组,相当于caling 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);
    }
}

数组在技术上更快,但它们不是对象.这取决于您希望在何处使用结果.大多数情况下,阵列就足够了.

脚本宝典总结

以上是脚本宝典为你收集整理的php – codeigniter,result()与result_array()全部内容,希望文章能够帮你解决php – codeigniter,result()与result_array()所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: