]> granicus.if.org Git - php/commitdiff
add function array_product()
authorAndrey Hristov <andrey@php.net>
Wed, 11 May 2005 11:43:11 +0000 (11:43 +0000)
committerAndrey Hristov <andrey@php.net>
Wed, 11 May 2005 11:43:11 +0000 (11:43 +0000)
ext/standard/array.c
ext/standard/basic_functions.c
ext/standard/php_array.h

index 7459e4f9660af35b93aa0074d9af8753937c67be..482803019b455c699024aea9e21d252751d03f8d 100644 (file)
@@ -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)
index d524f159a78c0dc3119a71ec9d8cb6713c6539dc..dd6893ff6a4ab9f5c50df060504dee7798ab35ce 100644 (file)
@@ -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)
index 46bd2c77dd55e392dbfe49452bfc6c1d377edc92..f949b38cb5f515317d9a0b32beada3f2bd6f5d12 100644 (file)
@@ -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);