ID: 45780
User updated by: Ultrasick at gmx dot de
-Summary: why is it only expected for the user data parameter?
Reported By: Ultrasick at gmx dot de
Status: Bogus
Bug Type: Scripting Engine problem
Operating System: probably any
PHP Version: 5.2.6
New Comment:
sorry, didn't want to change the summary
Previous Comments:
------------------------------------------------------------------------
[2008-08-11 22:48:48] Ultrasick at gmx dot de
So do I also need to add a "&" before the "$my_array" to make changes
in array and not in a copy of the array? Like this:
array_walk(&$my_array, 'my_function', &$my_variable);
------------------------------------------------------------------------
[2008-08-11 18:23:34] [EMAIL PROTECTED]
------------------------------------------------------------------------
[2008-08-11 18:22:29] [EMAIL PROTECTED]
This is expected.
> array_walk($my_array, 'my_function', $my_variable);
The variable is passed by reference to my_function(), you can verify
that by adding an echo in the function.
But as you did not passed $my_variable by reference, array_walk() uses
its own copy of $my_variable, and this is that copy which is passed to
my_function().
So you need to pass it explicitly by reference to array_walk().
------------------------------------------------------------------------
[2008-08-11 03:45:45] Ultrasick at gmx dot de
Description:
------------
The function "array_walk" ignores to pass the reference and passes the
content instead for the parameter "userdata" if the "&" isn't written
inside the "array_walk()" but inside the function definition ("function
somename(&$value, &$key, &$userdata)"). This doesn't work for the
"userdata"-parameter but it does work for the "value" and the
"key"-parameter.
Have a look at the code at the bottom and the expected and the actual
result. Now compare what would happen if you would call "my_function"
manually by using
my_function($any_value, $any_key, $my_variable);
echo $my_variable;
and then using
my_function($any_value, $any_key, &$my_variable);
echo $my_variable;
In both cases $my_variable would be increased, of course.
So in my oppinion the userdata-problem it's not just inconsistency.
It's a bug because a part of the function definition is beeing ignored.
Reproduce code:
---------------
<?
function my_function(&$value, &$key, &$userdata){
$userdata++;
}
$my_array = array('one', 'two', 'three');
$my_variable = 1;
array_walk($my_array, 'my_function', $my_variable);
echo $my_variable;
echo '<p><hr></p>';
array_walk($my_array, 'my_function', &$my_variable);
echo $my_variable;
?>
Expected result:
----------------
expected output:
4
-----------
4
Actual result:
--------------
actual output:
1
-----------
4
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=45780&edit=1