|  | Posted by Hendri Kurniawan on 04/20/07 04:29 
Steve wrote:> 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
 >
 >
 >
 
 Both of the code you posted did not work on my box (php5.2) unmodified.
 
 My initial thought will be using foreach on the array $numbers, but you
 might have your own reason not to use it.
 
 
 Hendri Kurniawan
  Navigation: [Reply to this message] |