]> granicus.if.org Git - php/commitdiff
Added array_sum() function.
authorAndrei Zmievski <andrei@php.net>
Wed, 25 Oct 2000 17:40:11 +0000 (17:40 +0000)
committerAndrei Zmievski <andrei@php.net>
Wed, 25 Oct 2000 17:40:11 +0000 (17:40 +0000)
NEWS
TODO
ext/standard/array.c
ext/standard/basic_functions.c
ext/standard/php_array.h

diff --git a/NEWS b/NEWS
index c7121aca302a50cf2d45babeb4fdcacc03c2f1c8..3b2e1b82554200428fb9f55a28ab195e52be6aac 100644 (file)
--- 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 5afc8c10bbdcd4957894441d6d2761b786fcba7c..8d8aaab93f166fc6580ae018007f9b20a7e33655 100644 (file)
--- 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
index f6b9ae5708e2896aedce2f2903a4c9e8b1e3f0b6..80a22395727467656863c1a2175bdfbf1bc3aa9b 100644 (file)
@@ -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
index 1266b9494aa4c23d2bbab18641080074ab69d5ed..880f6b63f03888e6bc708d131ba09677e3e8c741 100644 (file)
@@ -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)
index c080457afbab5fbe5a376e85856a2dd2766e7545..b02515e37845f0498df22f60220699a9176530a9 100644 (file)
@@ -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);