]> granicus.if.org Git - php/commitdiff
Fix the recursive counting, it was broken for associative or non-sequential
authorAndrei Zmievski <andrei@php.net>
Thu, 10 Jan 2002 21:31:06 +0000 (21:31 +0000)
committerAndrei Zmievski <andrei@php.net>
Thu, 10 Jan 2002 21:31:06 +0000 (21:31 +0000)
arrays. Also update NEWS file.

NEWS
ext/standard/array.c

diff --git a/NEWS b/NEWS
index 674ee0b0db90be682b93608a60e019da572f461c..a4a015b2a0558be0dc53f34ea2724c8d1d9a6e16 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,7 +1,9 @@
 PHP 4                                                                      NEWS
 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 ?? ??? 200?, Version 4.2.0-dev
-- added mb_get_info() to get internal settings of mbstring.
+- Added second parameter to count() that can be used to specify either normal
+  or recursive counting. (patch by Vlad Bosinceanu <glipy@fx.ro>)
+- Added mb_get_info() to get internal settings of mbstring.
 - Added async query functions to PostgreSQL module (Yasuo)
 - Added pg_copy_to()/pg_copy_from() for PostgreSQL module (Youichi, Yasuo)
 - Added IPv6 support in FTP extension. (Stig Venaas)
index 86331a75e8ac90e75682f64813e88d3d0ea9938c..a1daeacecda181a111d6a461a847541c2c1a79cf 100644 (file)
@@ -230,7 +230,7 @@ PHP_FUNCTION(ksort)
 
 int php_count_recursive(zval *array, long mode)
 {
-       long cnt = 0, i;
+       long cnt = 0;
        zval **element;
        
        HashTable *target_hash;
@@ -240,13 +240,16 @@ int php_count_recursive(zval *array, long mode)
        {
                cnt += zend_hash_num_elements(target_hash);
                if (mode == COUNT_RECURSIVE) {
-                       for(i = 0; i < zend_hash_num_elements(target_hash); i++) {
-                               if (zend_hash_index_find (Z_ARRVAL_P(array), i, (void **) &element) == SUCCESS) {
-                                       cnt += php_count_recursive(*element, COUNT_RECURSIVE);
-                               }
+                       HashPosition pos;
+
+                       for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(array), &pos);
+                                zend_hash_get_current_data_ex(Z_ARRVAL_P(array), (void **) &element, &pos) == SUCCESS;
+                                zend_hash_move_forward_ex(Z_ARRVAL_P(array), &pos)) {
+                               cnt += php_count_recursive(*element, COUNT_RECURSIVE);
                        }
                }
        }
+
        return cnt;
 }