doukui4786 2019-06-06 01:08
浏览 62
已采纳

将数组传递给CodeIgniter中的系统核心Controller.php

I am in the process of updating my CodeIgniter app from version 2.x to 3.1. Previously, I used to call this function to count the number of non-zero numbers in the passed-in array:

function getCountOfNonZero($array, $d) {
    $NonZeroCount = 0;
    foreach($array as $key=>$value) {
        if(is_array($value)) {
            $d = $key;
            $NonZeroCount += getCountOfNonZero($value, $d); }
        else {
              if($value<>0 & $key<>'RES') {
                $NonZeroCount++;
            }
        }
    }
    return $NonZeroCount;
}

Now that I am trying to call this function in version 3.1 of CodeIgniter, I am getting an error. This is how I call the function:

echo "Count of non-zero is: ".$controller->getCountOfNonZero($myArray);

One thing that is different is that the function is in system\core\Controller.php but this is returning an error whenever I call the function and try to pass in the array.

Is there any reason why I can't pass an array to the core system controller in CodeIgniter?

  • 写回答

1条回答 默认 最新

  • douqianxian7008 2019-06-06 01:48
    关注

    Now that you have moved the function into a class you you will need to use $this->getCountOfNonZero($value, $d); when calling the function from within itself.

    function getCountOfNonZero($array, $d) {
        $NonZeroCount = 0;
        foreach($array as $key=>$value) {
            if(is_array($value)) {
                $d = $key;
                $NonZeroCount += $this->getCountOfNonZero($value, $d); }
            else {
                  if($value<>0 & $key<>'RES') {
                    $NonZeroCount++;
                }
            }
        }
        return $NonZeroCount;
    }
    

    Personally I would probably just put the method into a helper file and autoload it, rather than tethering it to your core controller.


    Creating a helper method:

    1. As your method relates to arrays, we could just extend the already existing array helper by creating the following file.

    application/helpers/MY_array_helper.php

    1. Add your original method to the file.

    2. Auto-load it during system initialization. This is done by opening the application/config/autoload.php file and adding the helper to the autoload array.

    3. Usage it anywhere in your application:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 请问如何查看手机root记录?
  • ¥15 商城小程序订单号重复
  • ¥15 学校优化算法sbo和蚁群算法怎么结合
  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素
  • ¥15 stc12c5a60s2、QMC5883L、LCD1602组合测量磁场所需程序