php - 如何在codeigniter中使用回调函数

标签 php codeigniter callback

我有一个数组$services,这个数组包含以下值:

Array
(
  [0] => One way
  [1] =>  Hourly
  [2] =>  To Airport
  [3] =>  From Airport
  [4] =>   Birthday
  [5] =>  Wedding
  [6] =>  Concert
  [7] =>   Sporting Event
  [8] =>   Cruise Party
  [9] =>  Funeral
)

您会注意到数组中的某些值包含空格。
为了从数组值中删除这个空间,我创建了一个 array_walk 函数,它进入数组并修剪空白。

public function trim_value(&$value) {
     $value = trim($value);
}  

众所周知,array_walk 函数的语法是:

array_walk($array, 'callback_function');

现在我想在 codeigniter Controller 中使用这个函数。据我所知, Controller 中的一个函数被用作另一个函数中的 $this-> 函数。
所以我尝试将回调函数用作:

array_walk($services, $this->trim_value);

它总是抛出以下错误:

A PHP Error was encountered

<p>Severity: Notice</p>
<p>Message:  Undefined property: Attribute::$trim_value</p>
<p>Filename: controllers/attribute.php</p>
<p>Line Number: 230</p>

那么有人能告诉我如何在 Controller 的一个函数中使用回调函数吗?

最佳答案

您必须向 array_walk() 传递一个包含 $this 的数组作为已定义的 变量/指针

试试这个

array_walk($array, array($this, 'trim_value'));

关于php - 如何在codeigniter中使用回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29620081/

相关文章:

php - 在常量中添加变量

php - 使用 window.location 重定向发送 $_SESSION 变量

security - Http Post Encryption,Codeigniter enc 类?可能的?

php - undefined offset 通知似乎错误

php - 我的 codeigniter 无法在 centos 上运行(内部服务器错误 500)

javascript - session 超时时无法在页面回调中调用 Response.Redirect?

c++ - 关于对抗逆变的问题。回调相关问题

php - 无法将 Laravel 5.4 更新到 5.5 Composer

php - 子文件夹中的 wordpress htaccess 未重定向到 https

django - 回调中使用了错误的 sessionID,但仅限于一台特定计算机