#34982 [Opn]: array_walk_recursive() modifies elements outside function scope
| From: | tony2001@php.net | Date: | Tue, 25 Oct 2005 19:57:36 +0000 |
| Subject: | #34982 [Opn]: array_walk_recursive() modifies elements outside function scope | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-87036@lists.php.net to get a copy of this message | ||
ID: 34982
Updated by: tony2001@php.net
Reported By: curt@php.net
Status: Open
Bug Type: Arrays related
Operating System: *
PHP Version: 5CVS-2005-10-25 (CVS)
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[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