summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrey Hristov <andrey@php.net>2005-05-11 11:43:11 +0000
committerAndrey Hristov <andrey@php.net>2005-05-11 11:43:11 +0000
commitbe3a9ccdf325ad6247f2c90bd39005cd4050d20b (patch)
tree56f2714d58cdf4b9a76b0334e4cf697896fe6194
parent4cabeaf1c771a413b4df5524b4866ece179cca38 (diff)
downloadphp-git-be3a9ccdf325ad6247f2c90bd39005cd4050d20b.tar.gz
add function array_product()
-rw-r--r--ext/standard/array.c47
-rw-r--r--ext/standard/basic_functions.c1
-rw-r--r--ext/standard/php_array.h1
3 files changed, 49 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)
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index d524f159a7..dd6893ff6a 100644
--- a/ext/standard/basic_functions.c
+++ b/ext/standard/basic_functions.c
@@ -813,6 +813,7 @@ function_entry basic_functions[] = {
PHP_FE(array_diff_uassoc, NULL)
PHP_FE(array_udiff_uassoc, NULL)
PHP_FE(array_sum, NULL)
+ PHP_FE(array_product, NULL)
PHP_FE(array_filter, NULL)
PHP_FE(array_map, NULL)
PHP_FE(array_chunk, NULL)
diff --git a/ext/standard/php_array.h b/ext/standard/php_array.h
index 46bd2c77dd..f949b38cb5 100644
--- a/ext/standard/php_array.h
+++ b/ext/standard/php_array.h
@@ -92,6 +92,7 @@ PHP_FUNCTION(array_udiff_assoc);
PHP_FUNCTION(array_diff_uassoc);
PHP_FUNCTION(array_udiff_uassoc);
PHP_FUNCTION(array_sum);
+PHP_FUNCTION(array_product);
PHP_FUNCTION(array_filter);
PHP_FUNCTION(array_map);
PHP_FUNCTION(array_key_exists);