php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62264 array_walk not changeing keys
Submitted: 2012-06-08 09:32 UTC Modified: 2012-06-09 00:26 UTC
From: marti dot markov at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: master-Git-2012-06-08 (Git) OS: Mac OS X
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
32 - 25 = ?
Subscribe to this entry?

 
 [2012-06-08 09:32 UTC] 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');

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-06-08 12:41 UTC] 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 23:27 UTC] 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 23:27 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2012-06-09 00:26 UTC] marti dot markov at gmail dot com
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).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 18:01:28 2024 UTC