【问题标题】:Undefined offset in own helper codeigniter [closed]自己的助手codeigniter中未定义的偏移量[关闭]
【发布时间】:2013-04-16 15:40:47
【问题描述】:

我想在数组值$stopwords前面连接字符“/\b”,在后面连接字符“\b/i”。

我的帮手

function addRegex_SW($arrayIn = array()){
    $arrayOut = array();
    $count = count($arrayIn);   
    for ($i = 1; $i <= $count; $i++)
    {
       $char1 = "/\b"; 
       $char2 = "\b/i";
       $arrayOut[$i] = $char1.$arrayIn[$i].$char2;
       //echo $arrayOut[$i];
    }
return $arrayOut;
}

我的看法

$stopwords_Regex = addRegex_SW($stopwords);

然后当我运行代码时

A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 317
Filename: helpers/addRegex_helper.php
Line Number: 11

是哪一行

$arrayOut[$i] = $char1.$arrayIn[$i].$char2;

我确定我有谷歌它,但我仍然没有得到它。 谢谢

【问题讨论】:

    标签: php regex codeigniter helper


    【解决方案1】:

    您的for-loop 是逐个

    更改:for ($i = 1; $i &lt;= $count; $i++)

    收件人:for ($i = 0; $i &lt; $count; $i++)

    【讨论】:

    • 哦我忘了数组索引必须从0开始,谢谢
    【解决方案2】:

    你不需要使用 for,使用 foreach 代替:

    function addRegex_SW($arrayIn = array()) {
        $arrayOut = array();
        foreach ($arrayIn as $value) {
            $arrayOut[] = '/\b' . $value . '\b/i';
        }
        return $arrayOut;
    }
    

    【讨论】:

    • 感谢您纠正循环
    猜你喜欢
    • 2013-06-22
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2012-03-28
    • 2018-10-26
    • 1970-01-01
    相关资源
    最近更新 更多