Bug #62264 [Nab]: array_walk not changeing keys
| From: | marti dot markov at gmail dot com | Date: | Sat, 09 Jun 2012 00:26:37 +0000 |
| Subject: | Bug #62264 [Nab]: array_walk not changeing keys | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-171159@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62264&edit=1
ID: 62264
User updated by: marti dot markov at gmail dot com
Reported by: marti dot markov at gmail dot com
Summary: array_walk not changeing keys
Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: Mac OS X
PHP Version: master-Git-2012-06-08 (Git)
Block user comment: N
Private report: N
New Comment:
But if it a reference to the variable why should it be change-able? I mean if I pass $key then I
understand that I can't change it for example but I'm passing the reference of the key or
am I just passing a reference to a clone of the key with &$key?
Anyways as I pass a reference it should be expected that if it is changed the variable itself will
be changed as well. Therefor the key in the array should be changed (looking at this from ref point
of view).
Previous Comments:
------------------------------------------------------------------------
[2012-06-08 23:27:09] nikic@php.net
As Sjon mentioned this is not a bug. You can't change they key in the callback. You generally
can't change the key in a dictionary without a) removing the old key and b) inserting the value
under a new key. This behavior is inherent to dictionaries.
------------------------------------------------------------------------
[2012-06-08 12:41:55] Sjon at hortensius dot net
The manual says "Only the values of the array may potentially be changed; .... If
the callback does not respect this requirement, the behavior of this function is
undefined, and unpredictable."
I think this could be a feature request, but not really a bug?
Also, your test-script is incomplete; but PHP's behavious is pretty consistent
regarding this functionality: http://3v4l.org/1dn4f
------------------------------------------------------------------------
[2012-06-08 09:32:25] marti dot markov at gmail dot com
Description:
------------
I have found this problem with 5.3.10 and got the latest GIT head revision and the problem was still
there.
When I try and change the value of the key it will not update it in the array, even though that I
use the key as a reference.
Test script:
---------------
$array = ( 'a' => 'Hello', 'b' => 'World');
array_walk($array, 'adda');
function adda(&$value, &$key) {
$key = "a".$key;
}
Expected result:
----------------
$array = ( 'aa' => 'Hello', 'ab' => 'World');
Actual result:
--------------
$array = ( 'a' => 'Hello', 'b' => 'World');
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62264&edit=1