iliaa           Wed Feb  5 12:56:08 2003 EDT

  Modified files:              
    /php4/ext/standard  php_array.h basic_functions.c array.c 
  Log:
  Added array_walk_recursive() function that can apply array_walk recursively
  to an array.
  
  
Index: php4/ext/standard/php_array.h
diff -u php4/ext/standard/php_array.h:1.41 php4/ext/standard/php_array.h:1.42
--- php4/ext/standard/php_array.h:1.41  Mon Jan 13 13:12:23 2003
+++ php4/ext/standard/php_array.h       Wed Feb  5 12:56:08 2003
@@ -19,7 +19,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: php_array.h,v 1.41 2003/01/13 18:12:23 andrey Exp $ */
+/* $Id: php_array.h,v 1.42 2003/02/05 17:56:08 iliaa Exp $ */
 
 #ifndef PHP_ARRAY_H
 #define PHP_ARRAY_H
@@ -39,6 +39,7 @@
 PHP_FUNCTION(uasort);
 PHP_FUNCTION(uksort);
 PHP_FUNCTION(array_walk);
+PHP_FUNCTION(array_walk_recursive);
 PHP_FUNCTION(count);
 PHP_FUNCTION(end);
 PHP_FUNCTION(prev);
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.570 
php4/ext/standard/basic_functions.c:1.571
--- php4/ext/standard/basic_functions.c:1.570   Mon Feb  3 16:48:36 2003
+++ php4/ext/standard/basic_functions.c Wed Feb  5 12:56:08 2003
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: basic_functions.c,v 1.570 2003/02/03 21:48:36 iliaa Exp $ */
+/* $Id: basic_functions.c,v 1.571 2003/02/05 17:56:08 iliaa Exp $ */
 
 #include "php.h"
 #include "php_streams.h"
@@ -813,6 +813,7 @@
        PHP_FE(uksort,                                  first_arg_force_ref)
        PHP_FE(shuffle,                                 first_arg_force_ref)
        PHP_FE(array_walk,                              first_arg_force_ref)
+       PHP_FE(array_walk_recursive,                    first_arg_force_ref)
        PHP_FE(count,                                                                  
                                                 NULL)
        PHP_FE(end,                                             first_arg_force_ref)
        PHP_FE(prev,                                    first_arg_force_ref)
Index: php4/ext/standard/array.c
diff -u php4/ext/standard/array.c:1.218 php4/ext/standard/array.c:1.219
--- php4/ext/standard/array.c:1.218     Mon Feb  3 11:57:02 2003
+++ php4/ext/standard/array.c   Wed Feb  5 12:56:08 2003
@@ -21,7 +21,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: array.c,v 1.218 2003/02/03 16:57:02 sniper Exp $ */
+/* $Id: array.c,v 1.219 2003/02/05 17:56:08 iliaa Exp $ */
 
 #include "php.h"
 #include "php_ini.h"
@@ -943,7 +943,7 @@
 }
 /* }}} */
 
-static int php_array_walk(HashTable *target_hash, zval **userdata TSRMLS_DC)
+static int php_array_walk(HashTable *target_hash, zval **userdata, int recursive 
+TSRMLS_DC)
 {
        zval **args[3],                 /* Arguments to userland function */
                  *retval_ptr,                  /* Return value - unused */
@@ -961,33 +961,44 @@
 
        /* Iterate through hash */
        while (zend_hash_get_current_data_ex(target_hash, (void **)&args[0], &pos) == 
SUCCESS) {
-               /* Allocate space for key */
-               MAKE_STD_ZVAL(key);
-
-               /* Set up the key */
-               if (zend_hash_get_current_key_ex(target_hash, &string_key, 
&string_key_len, &num_key, 0, &pos) == HASH_KEY_IS_LONG) {
-                       Z_TYPE_P(key) = IS_LONG;
-                       Z_LVAL_P(key) = num_key;
-               } else {
-                       ZVAL_STRINGL(key, string_key, string_key_len-1, 1);
-               }
-               
-               /* Call the userland function */
-               if (call_user_function_ex(EG(function_table), NULL, 
*BG(array_walk_func_name),
-                                                  &retval_ptr, userdata ? 3 : 2, 
args, 0, NULL TSRMLS_CC) == SUCCESS) {
-               
-                       zval_ptr_dtor(&retval_ptr);
+               if (recursive && Z_TYPE_PP(args[0]) == IS_ARRAY) {
+                       HashTable *thash;
+                       
+                       thash = HASH_OF(*(args[0]));
+                       if (thash == target_hash) {
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion 
+detected");
+                               return 0;
+                       }
+                       php_array_walk(thash, userdata, recursive TSRMLS_CC);
                } else {
-                       char *func_name;
+                       /* Allocate space for key */
+                       MAKE_STD_ZVAL(key);
 
-                       if (zend_is_callable(*BG(array_walk_func_name), 0, 
&func_name)) {
-                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to 
call %s()", func_name);
+                       /* Set up the key */
+                       if (zend_hash_get_current_key_ex(target_hash, &string_key, 
+&string_key_len, &num_key, 0, &pos) == HASH_KEY_IS_LONG) {
+                               Z_TYPE_P(key) = IS_LONG;
+                               Z_LVAL_P(key) = num_key;
                        } else {
-                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to 
call %s() - function does not exist", func_name);
+                               ZVAL_STRINGL(key, string_key, string_key_len-1, 1);
                        }
+               
+                       /* Call the userland function */
+                       if (call_user_function_ex(EG(function_table), NULL, 
+*BG(array_walk_func_name),
+                                                          &retval_ptr, userdata ? 3 : 
+2, args, 0, NULL TSRMLS_CC) == SUCCESS) {
+               
+                               zval_ptr_dtor(&retval_ptr);
+                       } else {
+                               char *func_name;
 
-                       efree(func_name);
-                       break;
+                               if (zend_is_callable(*BG(array_walk_func_name), 0, 
+&func_name)) {
+                                       php_error_docref(NULL TSRMLS_CC, E_WARNING, 
+"Unable to call %s()", func_name);
+                               } else {
+                                       php_error_docref(NULL TSRMLS_CC, E_WARNING, 
+"Unable to call %s() - function does not exist", func_name);
+                               }
+
+                               efree(func_name);
+                               break;
+                       }
                }
 
                zval_ptr_dtor(&key);
@@ -1026,11 +1037,46 @@
                BG(array_walk_func_name) = old_walk_func_name;
                RETURN_FALSE;
        }
-       php_array_walk(target_hash, userdata TSRMLS_CC);
+       php_array_walk(target_hash, userdata, 0 TSRMLS_CC);
+       BG(array_walk_func_name) = old_walk_func_name;
+       RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto bool array_walk_recursive(array input, string funcname [, mixed 
+userdata])
+   Apply a user function recursively to every member of an array */
+PHP_FUNCTION(array_walk_recursive)
+{
+       int     argc;
+       zval **array,
+                **userdata = NULL,
+                **old_walk_func_name;
+       HashTable *target_hash;
+
+       argc = ZEND_NUM_ARGS();
+       old_walk_func_name = BG(array_walk_func_name);
+       if (argc < 2 || argc > 3 ||
+               zend_get_parameters_ex(argc, &array, &BG(array_walk_func_name), 
+&userdata) == FAILURE) {
+               BG(array_walk_func_name) = old_walk_func_name;
+               WRONG_PARAM_COUNT;
+       }
+       target_hash = HASH_OF(*array);
+       if (!target_hash) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "The argument should be an 
+array");
+               BG(array_walk_func_name) = old_walk_func_name;
+               RETURN_FALSE;
+       }
+       if (Z_TYPE_PP(BG(array_walk_func_name)) != IS_ARRAY && 
+Z_TYPE_PP(BG(array_walk_func_name)) != IS_STRING) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Wrong syntax for function 
+name");
+               BG(array_walk_func_name) = old_walk_func_name;
+               RETURN_FALSE;
+       }
+       php_array_walk(target_hash, userdata, 1 TSRMLS_CC);
        BG(array_walk_func_name) = old_walk_func_name;
        RETURN_TRUE;
 }
 /* }}} */
+
 
 /* void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior)
  *       0 = return boolean



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

Reply via email to