php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #65872 array_reduce() callback should receive current key/index
Submitted: 2013-10-09 22:18 UTC Modified: 2013-10-09 22:20 UTC
Votes:39
Avg. Score:4.4 ± 0.8
Reproduced:37 of 38 (97.4%)
Same Version:28 (75.7%)
Same OS:28 (75.7%)
From: php at hotblocks dot nl Assigned:
Status: Open Package: Arrays related
PHP Version: Irrelevant OS: All
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 — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
40 + 39 = ?
Subscribe to this entry?

 
 [2013-10-09 22:18 UTC] php at hotblocks dot nl
Description:
------------
In most languages, the callback of Array's reduce() method receives the current index (as well as $result and $item). In PHP it doesn't. There's plenty of room for a third argument, since it's never used.

The test script assumes that 3rd argument.

Test script:
---------------
// Compare string lengths and remember index of the longest.

$strings = ['foo', 'bar', 'fooo', 'barry', 'barrr'];
$longest_index = array_reduce($strings, function($result, $string, $index) use ($strings) {
  return strlen($string) > strlen($strings[$result]) ? $index : $result;
}, 0);

// Re-key array depending on values.

$array = ['10' => 'a', '20' => 'b', '150' => 'c'];
$rekeyed = array_reduce($array, function($result, $el, $index) {
  $new_index = $el > 100 ? 'x' . $index : 'y' . $index;
  $result[$new_index] = $el;
  return $result;
}, []);


Expected result:
----------------
I expect $index in the callbacks to be present, and:

$longest_index should be 3 (for 'barry'), and

$rekeyed should have indexes 'x10', 'x20' and 'y150'.

Actual result:
--------------
Warnings and nils.

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-09 22:20 UTC] php at hotblocks dot nl
-Summary: array_reduce() callback should receive current key +Summary: array_reduce() callback should receive current key/index
 [2013-10-09 22:20 UTC] php at hotblocks dot nl
Summary+
 [2014-09-27 10:52 UTC] cameron dot adam+php at gmail dot com
If you were to implement this change (you should, it's a significant shortfall in the implementation, I think), then perhaps follow CFML's lead on these kind of functions. I've written an opinion on this in reaction to a stack overflow question, the solution to which could use array_reduce() nicely if it was implemented better: http://blog.adamcameron.me/2014/09/php-significant-oversight-in.html. I think CFML have nailed these functions well. The conceit is they not only pass the value, they also pass the index, and as well as that a ref to the entire array too (which is, sometimes, very handy to have for cross-entry analysis). I think the CFML approach would still fit in with the general PHP milieu here?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 13:01:28 2024 UTC