Req #65872 [NEW]: array_reduce() callback should receive current key

From: Date: Wed, 09 Oct 2013 22:18:29 +0000
Subject: Req #65872 [NEW]: array_reduce() callback should receive current key
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-182106@lists.php.net to get a copy of this message
From: php at hotblocks dot nl Operating system: All PHP version: Irrelevant Package: Arrays related Bug Type: Feature/Change Request Bug description:array_reduce() callback should receive current key 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. -- Edit bug report at https://bugs.php.net/bug.php?id=65872&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=65872&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=65872&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=65872&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=65872&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=65872&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=65872&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=65872&r=needscript Try newer version: https://bugs.php.net/fix.php?id=65872&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=65872&r=support Expected behavior: https://bugs.php.net/fix.php?id=65872&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=65872&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=65872&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=65872&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65872&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=65872&r=dst IIS Stability: https://bugs.php.net/fix.php?id=65872&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=65872&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=65872&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=65872&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=65872&r=mysqlcfg

« previous php.bugs (#182106) next »