【问题标题】:I cant get sum array of result_array codeigniter (get num_rows after result_array)我无法获得 result_array codeigniter 的总和数组(在 result_array 之后获得 num_rows)
【发布时间】:2019-07-29 07:42:44
【问题描述】:

我想获取 num_rows 的查询,但我需要从 result_array 而不是从查询中获取。所以,我调用函数 array_sum 来计算 num_rows 但它不起作用

我尝试使用 array_sum(php 函数)但它返回 0,它应该返回 3

ListModel.php

Class ListModel extends CI_Model {
    public function getList($id=null){
        $listm = $this->db->query("
                    SELECT * FROM m_list m
                    LEFT JOIN list_country lc ON lc.id=m.id_country
                    WHERE lc.id_section = $id"
        )->result_array();
        return $listm;
    }
}

MController.php

$arr_m= $this->ListModel->getList(15);

$return['total'] = array_sum($arr_m);

有什么办法解决吗?

【问题讨论】:

    标签: arrays codeigniter sum codeigniter-3


    【解决方案1】:

    您需要使用 countsizeof 函数来获取数组的长度。

    $return['total'] = count($arr_m);
    

    【讨论】:

      【解决方案2】:

      缺少右双引号

      Class ListModel extends CI_Model {
              public function getList($id=null){
                  $listm = $this->db->query("
                              SELECT * FROM m_list m
                              LEFT JOIN list_country lc ON lc.id=m.id_country
                              WHERE lc.id_section = $id
                  ")->result_array();
                  return $listm;
              }
          }
      

      【讨论】:

      • 对不起,我忘记复制了。列表很好并返回我需要的 3 个数组,问题是我需要总计但它返回 0
      猜你喜欢
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多