I'd share the array_sum() code with array_product() code.. They're identical except for + being * in latter. (I guess you simply copy'pasted it? :)

    --Jani


On Wed, 11 May 2005, Andrey Hristov wrote:

andrey          Wed May 11 07:43:14 2005 EDT

 Modified files:
   /php-src/ext/standard        basic_functions.c php_array.h array.c
 Log:
 add function array_product()


http://cvs.php.net/diff.php/php-src/ext/standard/basic_functions.c?r1=1.714&r2=1.715&ty=u Index: php-src/ext/standard/basic_functions.c diff -u php-src/ext/standard/basic_functions.c:1.714 php-src/ext/standard/basic_functions.c:1.715 --- php-src/ext/standard/basic_functions.c:1.714 Tue May 10 08:50:53 2005 +++ php-src/ext/standard/basic_functions.c Wed May 11 07:43:10 2005 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */

-/* $Id: basic_functions.c,v 1.714 2005/05/10 12:50:53 andrey Exp $ */
+/* $Id: basic_functions.c,v 1.715 2005/05/11 11:43:10 andrey Exp $ */

#include "php.h"
#include "php_streams.h"
@@ -813,6 +813,7 @@
        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)
http://cvs.php.net/diff.php/php-src/ext/standard/php_array.h?r1=1.48&r2=1.49&ty=u
Index: php-src/ext/standard/php_array.h
diff -u php-src/ext/standard/php_array.h:1.48 
php-src/ext/standard/php_array.h:1.49
--- php-src/ext/standard/php_array.h:1.48       Mon Nov  1 07:09:46 2004
+++ php-src/ext/standard/php_array.h    Wed May 11 07:43:11 2005
@@ -19,7 +19,7 @@
   +----------------------------------------------------------------------+
*/

-/* $Id: php_array.h,v 1.48 2004/11/01 12:09:46 helly Exp $ */
+/* $Id: php_array.h,v 1.49 2005/05/11 11:43:11 andrey Exp $ */

#ifndef PHP_ARRAY_H
#define PHP_ARRAY_H
@@ -92,6 +92,7 @@
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);
http://cvs.php.net/diff.php/php-src/ext/standard/array.c?r1=1.298&r2=1.299&ty=u
Index: php-src/ext/standard/array.c
diff -u php-src/ext/standard/array.c:1.298 php-src/ext/standard/array.c:1.299
--- php-src/ext/standard/array.c:1.298  Mon Apr 25 02:13:57 2005
+++ php-src/ext/standard/array.c        Wed May 11 07:43:11 2005
@@ -21,7 +21,7 @@
   +----------------------------------------------------------------------+
*/

-/* $Id: array.c,v 1.298 2005/04/25 06:13:57 dmitry Exp $ */
+/* $Id: array.c,v 1.299 2005/05/11 11:43:11 andrey Exp $ */

#include "php.h"
#include "php_ini.h"
@@ -3918,9 +3918,56 @@
                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)



-- Donate @ http://pecl.php.net/wishlist.php/sniper

--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to