? ext/standard/tests/x
Index: ext/standard/array.c
===================================================================
RCS file: /repository/php-src/ext/standard/array.c,v
retrieving revision 1.460
diff -u -r1.460 array.c
--- ext/standard/array.c 10 Aug 2008 17:49:29 -0000 1.460
+++ ext/standard/array.c 16 Aug 2008 13:52:29 -0000
@@ -4354,7 +4354,7 @@
}
/* }}} */
-/* {{{ proto mixed array_reduce(array input, mixed callback [, int
initial]) U
+/* {{{ proto mixed array_reduce(array input, mixed callback [,
mixed initial]) U
Iteratively reduce the array to a single value via the callback.
*/
PHP_FUNCTION(array_reduce)
{
@@ -4365,18 +4365,19 @@
zval *retval;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;
- long initial;
+ zval *initial;
HashPosition pos;
HashTable *htbl;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "af|l",
&input, &fci, &fci_cache, &initial) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "af|z",
&input, &fci, &fci_cache, &initial) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS() > 2) {
ALLOC_ZVAL(result);
+ *result = *initial;
+ zval_copy_ctor(result);
INIT_PZVAL(result);
- ZVAL_LONG(result, initial);
} else {
MAKE_STD_ZVAL(result);
ZVAL_NULL(result);
Index: ext/standard/tests/array/array_reduce.phpt
===================================================================
RCS file: ext/standard/tests/array/array_reduce.phpt
diff -N ext/standard/tests/array/array_reduce.phpt
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ext/standard/tests/array/array_reduce.phpt 16 Aug 2008
13:52:29 -0000
@@ -0,0 +1,79 @@
+--TEST--
+Test array_reduce() function
+--INI--
+precision=14
+--FILE--
+<?php
+/* Prototype: array array_reduce(array $array, mixed $callback,
mixed $initial);
+ Description: Iteratively reduce the array to a single value via
the callback
+*/
+
+$array = array('foo', 'foo', 'bar', 'qux', 'qux', 'quux');
+
+echo "\n*** Testing array_reduce() to integer ***\n";
+function reduce_int($w, $v) { return $w + strlen($v); }
+$initial = 42;
+var_dump(array_reduce($array, 'reduce_int', $initial), $initial);
+
+echo "\n*** Testing array_reduce() to float ***\n";
+function reduce_float($w, $v) { return $w + strlen($v) / 10; }
+$initial = 4.2;
+var_dump(array_reduce($array, 'reduce_float', $initial), $initial);
+
+echo "\n*** Testing array_reduce() to string ***\n";
+function reduce_string($w, $v) { return $w . $v; }
+$initial = 'quux';
+var_dump(array_reduce($array, 'reduce_string', $initial), $initial);
+
+echo "\n*** Testing array_reduce() to array ***\n";
+function reduce_array($w, $v) { $w[$v]++; return $w; }
+$initial = array('foo' => 42, 'bar' => 17, 'qux' => -2, 'quux' =>
0);
+var_dump(array_reduce($array, 'reduce_array', $initial), $initial);
+
+echo "\n*** Testing array_reduce() to null ***\n";
+function reduce_null($w, $v) { return $w . $v; }
+$initial = null;
+var_dump(array_reduce($array, 'reduce_null', $initial), $initial);
+
+echo "\nDone";