Re: array_walk provides odd results
| From: | Lars Torben Wilson | Date: | Tue, 14 Nov 2000 04:07:46 +0000 |
| Subject: | Re: array_walk provides odd results | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-25104@lists.php.net to get a copy of this message | ||
Waldo L. Jaquith writes:
> Brian Clark wrote:
> > Waldo L. Jaquith wrote:
> >> 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.
> >>
> >> 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.
>
> > 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:
> [wheel reinventing omitted]
> > array_walk() and the above function produce the same output under PHP 3.0.18.
>
> Fortunately, this is an unusually simple wheel to reinvent. :) I imagine
> that anybody that uses your code will be able to achieve the same effect as
> the array_walk() function without any slowdown. And, unless I find out
> something about what's up with array_walk(), I believe I'll make use of your
> fine-looking wheel, which appears much closer to true than PHP's.
>
> Best,
> Waldo
The original code you posted makes it look like you expect the return
value of the function you're applying to the array elements to be
assigned into those elements; this is not the case. Also, since PHP
defaults to passing arguments by value, not reference, nothing
happens. What your code is doing is to walk over the elements of the
array, passing each element to the escape_quotes() function, which
then addslashes() the value that's been passed in, and returns the
result (which is discarded).
http://www.php.net/manual/function.array-walk.php
As noted in the manual:
Note: If func needs to be working with the actual values of the
array, specify that the first parameter of func should be
passed by reference. Then any changes made to those elements
will be made in the array itself.
So if you rewrite your original code to look something like this,
you'll have the correct answer:
<?php
error_reporting(E_ALL);
function escape_quotes(&$data) {
$data = addslashes($data);
}
$array[] = "''''";
$array[] = '""""';
array_walk($array, 'escape_quotes');
for ($i = 0; $i < count($array); $i++) {
echo $array[$i] . "<br>\n";
}
?>
I hope this clears it up,
Torben
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+