Re: array_walk provides odd results
| From: | Brian Clark | Date: | Tue, 14 Nov 2000 02:38:58 +0000 |
| Subject: | Re: array_walk provides odd results | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-25098@lists.php.net to get a copy of this message | ||
Waldo L. Jaquith wrote:
Folks, I'm trying to use array_walk to run a function on each element of an array. But it just isn't working. Below is a simple little script -- I define a function that runs AddSlashes, then I make a simple array, then array_walk it, and finally echo the contents of the (theoretically) now-slash-escaped array. ------------------------------- function escape_quotes($data) { $data = AddSlashes($data); return $data; } $array[] = "''''"; $array[] = '""""'; array_walk($array, 'escape_quotes'); for ($i=0;$i<count($array);$i++) { echo $array[$i]; } ------------------------------- I've tried this on v4.0.0 and v4.03pl1 with the same results in each: no slash-escaping of quotes. Having tried running other functions on the content (trim, reversing the string, etc.), I believe that AddSlashes is not a part of the problem. Can anybody offer any suggestions as to what I'm doing wrong with array_walk?Howdy Waldo, As far as I know, no one answered this, so sorry if it's already been covered. You can always try reinventing the wheel, although I haven't tried this under 4.x to make sure it produces the same output: <?php $array = array("''''",'""""'); function escape_quotes($data) {
$data = addslashes($data);
return $data;
}
function walk_array(&$array, $function)
{
while(list($i,) = each($array))
{
eval("\$array[$i] = \$function(\$array[$i]);");
}
return;
}
walk_array($array, 'escape_quotes');
for($i = 0; $i < count($array); $i++)
{
print $array[$i];} ?> array_walk() and the above function produce the same output under PHP 3.0.18. YMMV, -Brian