diff options
author | Andrey Hristov <andrey@php.net> | 2005-05-11 11:43:11 +0000 |
---|---|---|
committer | Andrey Hristov <andrey@php.net> | 2005-05-11 11:43:11 +0000 |
commit | be3a9ccdf325ad6247f2c90bd39005cd4050d20b (patch) | |
tree | 56f2714d58cdf4b9a76b0334e4cf697896fe6194 /ext/standard/array.c | |
parent | 4cabeaf1c771a413b4df5524b4866ece179cca38 (diff) | |
download | php-git-be3a9ccdf325ad6247f2c90bd39005cd4050d20b.tar.gz |
add function array_product()
Diffstat (limited to 'ext/standard/array.c')
-rw-r--r-- | ext/standard/array.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/ext/standard/array.c b/ext/standard/array.c index 7459e4f966..482803019b 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -3918,9 +3918,56 @@ PHP_FUNCTION(array_sum) Z_DVAL_P(return_value) += Z_DVAL(entry_n); } } +/* }}} */ +/* {{{ proto mixed array_product(array input) + Returns the product of the array entries */ +PHP_FUNCTION(array_product) +{ + zval **input, + **entry, + entry_n; + int argc = ZEND_NUM_ARGS(); + HashPosition pos; + double dval; + + if (argc != 1 || zend_get_parameters_ex(argc, &input) == FAILURE) { + WRONG_PARAM_COUNT; + } + + if (Z_TYPE_PP(input) != IS_ARRAY) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "The argument should be an array"); + return; + } + + ZVAL_LONG(return_value, 0); + + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(input), &pos); + zend_hash_get_current_data_ex(Z_ARRVAL_PP(input), (void **)&entry, &pos) == SUCCESS; + zend_hash_move_forward_ex(Z_ARRVAL_PP(input), &pos)) { + + if (Z_TYPE_PP(entry) == IS_ARRAY || Z_TYPE_PP(entry) == IS_OBJECT) + continue; + + entry_n = **entry; + zval_copy_ctor(&entry_n); + convert_scalar_to_number(&entry_n TSRMLS_CC); + + if (Z_TYPE(entry_n) == IS_LONG && Z_TYPE_P(return_value) == IS_LONG) { + dval = (double)Z_LVAL_P(return_value) * (double)Z_LVAL(entry_n); + if ( (double)LONG_MIN <= dval && dval <= (double)LONG_MAX ) { + Z_LVAL_P(return_value) *= Z_LVAL(entry_n); + continue; + } + } + convert_to_double(return_value); + convert_to_double(&entry_n); + Z_DVAL_P(return_value) *= Z_DVAL(entry_n); + } +} /* }}} */ + /* {{{ proto mixed array_reduce(array input, mixed callback [, int initial]) Iteratively reduce the array to a single value via the callback. */ PHP_FUNCTION(array_reduce) |