array_walk - 用户数据作为数组由ref [英] array_walk - userdata as array by ref

查看:48
本文介绍了array_walk - 用户数据作为数组由ref的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个我似乎无法处理的怪癖,只是黑客攻击。因为呼叫时间

by-reference是折旧的,我不想在php.ini中启用它,

我有点卡住了当我想要传递userdata作为数组byref

最初= array()。


//正在走的数组

$ numbers = array(1,56,999,1000,28,65);


//解决方案


//回调

函数validateInput(& $ value,$ key,& $ errors)

{

$ maxValue = 999;

if($ value< = $ maxValue)return;

$ errors [1] [$ key] =''元素''。 $关键。 ''必须是0

和''之间的整数。 $ maxValue。 ''。';

}

//黑客

$ error = array();

$ errors = array('''',& $ error);

array_walk($ numbers,''validateInput'',$ errors);

$ errors = $ errors [1];

print_r($ errors);


//我想做什么


//回调

函数validateInput(& $ value,$ key,& $ errors)

{

$ maxValue = 999;

if($ value< = $ maxValue)return;

$ errors [$ key] =''元素''。 $关键。 ''必须是介于0和

'之间的整数。 $ maxValue。 ''。';

}

$ errors = array();

array_walk($ numbers,''validateInput'',$错误);

print_r($ errors);

似乎php没有为$ error分配内存

定义为一个空数组,因为它没有数据(!isset),因此

有一个指向任何东西的指针(比喻)。我假设因为$ errors =

array('''',& $ error)为结构分配内存,然后回调就有了b $ b。 ''''实际上是触发分配的东西(使得我估计为\0
的空间)。当我想出黑客时,这是我的推理,

但是我想知道肯定。


这听起来是对的吗?关于获得结果的建议我看了

for?


tia,


me

/>

here''s a quirk i can''t seem to handle, just hack. since call-time
by-reference is depreciated and i don''t want to enable it in the php.ini,
i''m kind of stuck when i want to pass userdata as an array byref that is
initially = array().

// the array being walked
$numbers = array(1, 56, 999, 1000, 28, 65);

// the work-around

// the callback
function validateInput(&$value, $key, &$errors)
{
$maxValue = 999;
if ($value <= $maxValue) return;
$errors[1][$key] = ''Element '' . $key. '' must be a whole number between 0
and '' . $maxValue . ''.'';
}
// the hack
$error = array();
$errors = array('''', &$error);
array_walk($numbers, ''validateInput'', $errors);
$errors = $errors[1];
print_r($errors);

// what i''d like to do

// the callback
function validateInput(&$value, $key, &$errors)
{
$maxValue = 999;
if ($value <= $maxValue) return;
$errors[$key] = ''Element '' . $key. '' must be a whole number between 0 and
'' . $maxValue . ''.'';
}
$errors = array();
array_walk($numbers, ''validateInput'', $errors);
print_r($errors);
it seems as though php doesn''t allocate memory for $errors when it is
defined as an empty array since it has no data (!isset), and therefore
there''s a pointer to nothing (figuratively). i assume since $errors =
array('''', &$error) allocates memory for the structure, the callback then has
something to work on. the '''' being what actually triggers allocation (makes
room for \0 i guess). that was my reasoning when i came up with the hack,
but i''d like to know for sure.

does that sound about right? suggestions on getting the results i looking
for?

tia,

me

推荐答案

numbers = array(1,56,999,1000,28,65);


//解决方法


//回调

函数validateInput(&
numbers = array(1, 56, 999, 1000, 28, 65);

// the work-around

// the callback
function validateInput(&


value,


key,&
key, &


这篇关于array_walk - 用户数据作为数组由ref的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆