diff options
Diffstat (limited to 'ext/standard/array.c')
-rw-r--r-- | ext/standard/array.c | 39 |
1 files changed, 37 insertions, 2 deletions
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 |