From: cjbj at hotmail dot com Operating system: Windows 2000 PHP version: 4.3.2 PHP Bug Type: Arrays related Bug description: Array_reduce uses first element as default running total
Description: ------------ If the optional 'initial' parameter to array_reduce() is omitted, its default value is the first element of the array, and iteration proceeds over the second and subsequent array elements. This means that the first element may not have the "function" applied to it. Reproduce code: --------------- For example: <?php // See "Programming PHP (O'Reilly, March 2002)" on p128 function add_up($running_total, $current_value) { echo "running_total is $running_total, current_value is $current_value\n"; $running_total += $current_value * $current_value; return $running_total; } $numbers = array (2,3,5,7); $total = array_reduce($numbers, 'add_up'); print "Total is $total\n"; ?> Expected result: ---------------- The expected output is: running_total is 0, current_value is 2 running_total is 4, current_value is 3 running_total is 13, current_value is 5 running_total is 38, current_value is 7 Total is 87 This calls add_up() once per array element. Actual result: -------------- The actual result is: running_total is 2, current_value is 3 running_total is 11, current_value is 5 running_total is 36, current_value is 7 Total is 85 Adding the optional third parameter to array_reduce(): $total = array_reduce($numbers, 'add_up', 0); gives the expected (and desired for the default case) output: running_total is 0, current_value is 2 running_total is 4, current_value is 3 running_total is 13, current_value is 5 running_total is 38, current_value is 7 Total is 87 This total is the sum of the squares. The uncomfirmed errata to Programming PHP (http://www.oreilly.com/catalog/progphp/errata/progphp.unconfirmed) indicates to me the default value of 'initial' may be a bug. If it is not a bug, then the current behavior doesn't seem to match common expectations and the manual entry "applies iteratively the function function to the elements of the array input" could be clarified. Tested on 4.3.2 on Windows 2000 with the prebuilt binary of 29 June 2003 from php.net. -- Edit bug report at http://bugs.php.net/?id=24980&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=24980&r=trysnapshot4 Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=24980&r=trysnapshot5 Fixed in CVS: http://bugs.php.net/fix.php?id=24980&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=24980&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=24980&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=24980&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=24980&r=support Expected behavior: http://bugs.php.net/fix.php?id=24980&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=24980&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=24980&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=24980&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=24980&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=24980&r=dst IIS Stability: http://bugs.php.net/fix.php?id=24980&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=24980&r=gnused