传递用户数据作为参考在PHP中array_walk_recursive [英] Passing user data as reference to array_walk_recursive in Php
本文介绍了传递用户数据作为参考在PHP中array_walk_recursive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试着通过引用PHP的传递第三个参数 array_walk_recursive
I try to pass a third parameter via reference to Phps array_walk_recursive
$field = 'foo';
array_walk_recursive($config, function($value, $key, &$field) {
$field = 'bar';
}, $field);
echo $field // 'foo'
为什么 $字段
还是'富'
,虽然它已被传递给函数作为参考?
Why is $field
still 'foo'
, though it has been passed to the function as reference?
推荐答案
我要回答自己:
据匿名函数的 PHP文档继承了一个封闭的变量有在函数中定义与叶我的例子关键字使用
标题:
According to the php documentation of anonymous functions inherited variables of a closure have to be defined in the functions header with the keyword use
which leaves my example with:
function($value, $key) use (&$field) { ... }
虽然回调函数继承了与声明的参数使用
从其父从范围意味着/功能已申报(不执行)的
Though the callback function inherits the parameters declared with use
from its parent which means from the scope/function it has been declared (not executed) in.
这篇关于传递用户数据作为参考在PHP中array_walk_recursive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文