|
Posted by Steve on 04/20/07 03:57
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
Navigation:
[Reply to this message]
|