php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62749 array_map should call callback with array index
Submitted: 2012-08-04 19:14 UTC Modified: 2012-08-05 20:22 UTC
From: gmblar+php at gmail dot com Assigned:
Status: Wont fix Package: Arrays related
PHP Version: 5.4.5 OS: MacOSX
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: gmblar+php at gmail dot com
New email:
PHP Version: OS:

 

 [2012-08-04 19:14 UTC] gmblar+php at gmail dot com
Description:
------------
Add current array index as second argument to the callback of array_map

Test script:
---------------
<?php

$array = array(
    'foo' => 23,
    'bar' => 42
);

$result = array_map(function() {
    return implode(':', func_get_args());
}, $array);

var_dump($result);




Expected result:
----------------
array(2) {
  ["foo"]=>
  string(2) "23:foo"
  ["bar"]=>
  string(2) "42:foo"
}


Actual result:
--------------
array(2) {
  ["foo"]=>
  string(2) "23"
  ["bar"]=>
  string(2) "42"
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-04 19:19 UTC] gmblar+php at gmail dot com
Correct expected result:
----------------
array(2) {
  ["foo"]=>
  string(2) "23:foo"
  ["bar"]=>
  string(2) "42:bar"
}
 [2012-08-05 20:08 UTC] ajf at ajf dot me
Just do array_map($func, $array, array_keys($array));
 [2012-08-05 20:22 UTC] rasmus@php.net
Changing this would break any app currently using array_map() and the workaround 
is trivial.
 [2012-08-05 20:22 UTC] rasmus@php.net
-Status: Open +Status: Wont fix
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Mar 10 22:01:33 2025 UTC