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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2021 The PHP Group
All rights reserved.
Last updated: Thu Jan 28 07:01:23 2021 UTC