我有一个数组$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/