php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53736 array_walk key reference
Submitted: 2011-01-13 14:48 UTC Modified: 2011-01-13 16:02 UTC
From: samuel dot roze at gmail dot com Assigned:
Status: Wont fix Package: Arrays related
PHP Version: 5.3.5 OS: Redhat 5
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: samuel dot roze at gmail dot com
New email:
PHP Version: OS:

 

 [2011-01-13 14:48 UTC] samuel dot roze at gmail dot com
Description:
------------
The keys of arrays should be accessible by reference into walking functions. See the scripts below:

Test script:
---------------
$array = array(
        'COL1' => 'VALue1',
        'COL2' => '2',
        'COL4' => 'valUE3'
);
var_dump($array);

$walk = array_walk($array, function (&$value, &$key) {
        $key = strtolower($key);
        $value = strtolower($value);
});
var_dump($walk, $array);

Expected result:
----------------
array
  'COL1' => string 'VALue1' (length=6)
  'COL2' => string '2' (length=1)
  'COL4' => string 'valUE3' (length=6)

boolean true

array
  'col1' => string 'value1' (length=6)
  'col2' => string '2' (length=1)
  'col4' => string 'value3' (length=6)


Actual result:
--------------
array
  'COL1' => string 'VALue1' (length=6)
  'COL2' => string '2' (length=1)
  'COL4' => string 'valUE3' (length=6)

boolean true

array
  'COL1' => string 'value1' (length=6)
  'COL2' => string '2' (length=1)
  'COL4' => string 'value3' (length=6)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-13 16:02 UTC] cataphract@php.net
-Status: Open +Status: Wont fix
 [2011-01-13 16:02 UTC] cataphract@php.net
Array keys are not proper PHP values, so this is not possible.

There is no reasonable way to handle a callback function that did:

function (&$value, &$key) {
        $key = function () {};
        //or $key = array();
}
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Jan 26 10:01:23 2021 UTC