summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Zmievski <andrei@php.net>2000-10-25 17:40:11 +0000
committerAndrei Zmievski <andrei@php.net>2000-10-25 17:40:11 +0000
commita35bf41670bc2dd8f811ca66669969704db5bf65 (patch)
tree1e1a2641f64e9f8e247fa6f4bc0dca621ff02fca
parent1026416153413318b3e9393b941908c5770fbe8a (diff)
downloadphp-git-a35bf41670bc2dd8f811ca66669969704db5bf65.tar.gz
Added array_sum() function.
-rw-r--r--NEWS1
-rw-r--r--TODO2
-rw-r--r--ext/standard/array.c39
-rw-r--r--ext/standard/basic_functions.c1
-rw-r--r--ext/standard/php_array.h1
5 files changed, 41 insertions, 3 deletions
diff --git a/NEWS b/NEWS
index c7121aca30..3b2e1b8255 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,7 @@ PHP 4.0 NEWS
?? ??? 2000, Version 4.0.4
+- Added array_sum() function. (Andrei)
- Fixed a bug in session.c. The php_session_save_current_state did not check
if mod_data is NULL and such situation is possible if the user calls
session_module_name with a parameter. (alex@zend.com)
diff --git a/TODO b/TODO
index 5afc8c10bb..8d8aaab93f 100644
--- a/TODO
+++ b/TODO
@@ -77,7 +77,7 @@ ext/session
ext/standard
------------
- * array_sum(), array_mean()
+ * array_mean()
* add a version number to data serialized via serialize().
* array_add(). (Andrei)
* possibly modify parsing of GPC data to automatically create arrays if
diff --git a/ext/standard/array.c b/ext/standard/array.c
index f6b9ae5708..80a2239572 100644
--- a/ext/standard/array.c
+++ b/ext/standard/array.c
@@ -2276,7 +2276,6 @@ PHP_FUNCTION(array_intersect)
HashTable *hash;
int argc, i, c = 0;
Bucket ***lists, **list, ***ptrs, *p;
- zval *entry;
/* Get the argument count and check it */
argc = ARG_COUNT(ht);
@@ -2383,7 +2382,6 @@ PHP_FUNCTION(array_diff)
HashTable *hash;
int argc, i, c;
Bucket ***lists, **list, ***ptrs, *p;
- zval *entry;
/* Get the argument count and check it */
argc = ARG_COUNT(ht);
@@ -2770,6 +2768,43 @@ PHP_FUNCTION(array_rand)
}
/* }}} */
+/* {{{ proto mixed array_sum(array input)
+ Returns the sum of the array entries */
+
+PHP_FUNCTION(array_sum)
+{
+ zval **input,
+ **entry;
+ int argc = ZEND_NUM_ARGS();
+
+ if (argc != 1 || zend_get_parameters_ex(argc, &input) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ ZVAL_LONG(return_value, 0);
+
+ for (zend_hash_internal_pointer_reset(Z_ARRVAL_PP(input));
+ zend_hash_get_current_data(Z_ARRVAL_PP(input), (void **)&entry) == SUCCESS;
+ zend_hash_move_forward(Z_ARRVAL_PP(input))) {
+
+ if (Z_TYPE_PP(entry) == IS_ARRAY || Z_TYPE_PP(entry) == IS_OBJECT)
+ continue;
+
+ SEPARATE_ZVAL(entry);
+ convert_scalar_to_number(*entry);
+
+ if (Z_TYPE_PP(entry) == IS_LONG && Z_TYPE_P(return_value) == IS_LONG) {
+ Z_LVAL_P(return_value) += Z_LVAL_PP(entry);
+ } else {
+ convert_to_double(return_value);
+ convert_to_double_ex(entry);
+ Z_DVAL_P(return_value) += Z_DVAL_PP(entry);
+ }
+ }
+}
+
+/* }}} */
+
/*
* Local variables:
* tab-width: 4
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index 1266b9494a..880f6b63f0 100644
--- a/ext/standard/basic_functions.c
+++ b/ext/standard/basic_functions.c
@@ -549,6 +549,7 @@ function_entry basic_functions[] = {
PHP_FE(array_unique, NULL)
PHP_FE(array_intersect, NULL)
PHP_FE(array_diff, NULL)
+ PHP_FE(array_sum, NULL)
/* aliases from array.c */
PHP_FALIAS(pos, current, first_arg_force_ref)
diff --git a/ext/standard/php_array.h b/ext/standard/php_array.h
index c080457afb..b02515e378 100644
--- a/ext/standard/php_array.h
+++ b/ext/standard/php_array.h
@@ -72,6 +72,7 @@ PHP_FUNCTION(array_rand);
PHP_FUNCTION(array_unique);
PHP_FUNCTION(array_intersect);
PHP_FUNCTION(array_diff);
+PHP_FUNCTION(array_sum);
HashTable* php_splice(HashTable *, int, int, zval ***, int, HashTable **);
int multisort_compare(const void *a, const void *b);