php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45780 array_walk ignores to pass reference if & is in function definition at userdata
Submitted: 2008-08-11 03:45 UTC Modified: 2008-11-21 17:18 UTC
From: Ultrasick at gmx dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.6 OS: *
Private report: No CVE-ID: None
 [2008-08-11 03:45 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-11 18:22 UTC] lbarnaud@php.net
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 22:48 UTC] 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 23:03 UTC] Ultrasick at gmx dot de
sorry, didn't want to change the summary
 [2008-08-11 23:22 UTC] lbarnaud@php.net
array_walk is declared as follows:
bool array_walk ( array &$array , callback $funcname [, mixed
$userdata ] )

So $array is implicitly passed by reference.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 01:01:28 2024 UTC