#34982 [Opn->Csd]: array_walk_recursive() modifies elements outside function scope
| From: | dmitry@php.net | Date: | Fri, 28 Oct 2005 09:58:37 +0000 |
| Subject: | #34982 [Opn->Csd]: array_walk_recursive() modifies elements outside function scope | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-87175@lists.php.net to get a copy of this message | ||
ID: 34982
Updated by: dmitry@php.net
Reported By: curt@php.net
-Status: Open
+Status: Closed
Bug Type: Arrays related
Operating System: *
PHP Version: 5CVS-2005-10-25 (CVS)
-Assigned To:
+Assigned To: dmitry
New Comment:
Fixed in CVS HEAD, PHP_5_1 and PHP_5_0.
Previous Comments:
------------------------------------------------------------------------
[2005-10-26 04:52:58] curt@php.net
Mabey I didn't make the code clear but if you var_dump($a) right after
array_walk_recursive() all values have the value of 'changed'. The
problem is back outside of the function that called
array_walk_recursive(), the $ar variable should still retain the
original array values.
------------------------------------------------------------------------
[2005-10-25 21:57:35] tony2001@php.net
I'd say the bug is that it *doesn't* modify the first level elements,
because it's expected when you use &$arg instead of $arg.
------------------------------------------------------------------------
[2005-10-25 21:24:38] curt@php.net
Description:
------------
array_walk_recursive() modifies element values deeper than the first
level on vars outside the scope of the function call.
Reproduce code:
---------------
<?php
$ar = array(
'element 1',
array('element2 => subelement1')
);
func($ar);
var_dump($ar);
function func($a) {
array_walk_recursive($a, 'apply');
}
function apply(&$input, $key) {
$input = 'changed';
}
Expected result:
----------------
array(2) {
[0]=>
string(9) "element 1"
[1]=>
array(1) {
[0]=>
string(23) "element2 => subelement1"
}
}
Actual result:
--------------
array(2) {
[0]=>
string(9) "element 1"
[1]=>
array(1) {
[0]=>
string(7) "changed"
}
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=34982&edit=1