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
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC