From: Lars Strojny Date: Wed, 20 Aug 2008 20:35:16 +0000 (+0000) Subject: Allow mixed $initial (patch by Christian Seiler) X-Git-Tag: BEFORE_HEAD_NS_CHANGE~640 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fefef1621ffa6c8ee36407b5376229b29e48ad3e;p=php Allow mixed $initial (patch by Christian Seiler) --- diff --git a/ext/standard/array.c b/ext/standard/array.c index e5c2536f46..9b9fa23d5f 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -4296,7 +4296,7 @@ PHP_FUNCTION(array_product) } /* }}} */ -/* {{{ 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) { @@ -4307,18 +4307,19 @@ PHP_FUNCTION(array_reduce) 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); diff --git a/ext/standard/tests/array/array_reduce.phpt b/ext/standard/tests/array/array_reduce.phpt new file mode 100644 index 0000000000..7cf0f5ee19 --- /dev/null +++ b/ext/standard/tests/array/array_reduce.phpt @@ -0,0 +1,79 @@ +--TEST-- +Test array_reduce() function +--INI-- +precision=14 +--FILE-- + 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"; +?> +--EXPECTF-- +*** Testing array_reduce() to integer *** +int(61) +int(42) + +*** Testing array_reduce() to float *** +float(6.1) +float(4.2) + +*** Testing array_reduce() to string *** +unicode(23) "quuxfoofoobarquxquxquux" +unicode(4) "quux" + +*** Testing array_reduce() to array *** +array(4) { + [u"foo"]=> + int(44) + [u"bar"]=> + int(18) + [u"qux"]=> + int(0) + [u"quux"]=> + int(1) +} +array(4) { + [u"foo"]=> + int(42) + [u"bar"]=> + int(17) + [u"qux"]=> + int(-2) + [u"quux"]=> + int(0) +} + +*** Testing array_reduce() to null *** +unicode(19) "foofoobarquxquxquux" +NULL + +Done