cvs: php4 /ext/standard array.c basic_functions.c php_array.h

From: Date: Fri, 16 Mar 2001 20:46:35 +0000
Subject: cvs: php4 /ext/standard array.c basic_functions.c php_array.h
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-4619@lists.php.net to get a copy of this message
andrei Fri Mar 16 12:46:35 2001 EDT Modified files: /php4/ext/standard array.c basic_functions.c php_array.h Log: @- Added array_filter(), which allows filtering of array elements via @ the specified callback. (Andrei) Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.104 php4/ext/standard/array.c:1.105 --- php4/ext/standard/array.c:1.104 Fri Mar 16 11:51:08 2001 +++ php4/ext/standard/array.c Fri Mar 16 12:46:33 2001 @@ -21,7 +21,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: array.c,v 1.104 2001/03/16 19:51:08 andrei Exp $ */ +/* $Id: array.c,v 1.105 2001/03/16 20:46:33 andrei Exp $ */ #include "php.h" #include "php_ini.h" @@ -2887,6 +2887,82 @@ *return_value = *result; } +/* }}} */ + + +/* {{{ proto array array_filter(array input [, mixed callback]) + Filters elements from the array via the callback. */ +PHP_FUNCTION(array_filter) +{ + zval **input, **callback = NULL; + zval **operand; + zval **args[1]; + zval *retval = NULL; + char *callback_name; + char *string_key; + ulong string_key_len; + ulong num_key; + HashPosition pos; + + if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2 || + zend_get_parameters_ex(ZEND_NUM_ARGS(), &input, &callback) == FAILURE) { + WRONG_PARAM_COUNT; + } + + if (Z_TYPE_PP(input) != IS_ARRAY) { + php_error(E_WARNING, "%s() expects argument 1 to be an array", + get_active_function_name()); + return; + } + + if (ZEND_NUM_ARGS() > 1) { + if (!zend_is_callable(*callback, 0, &callback_name)) { + php_error(E_WARNING, "%s() expects argument 2, '%s', to be a valid callback", + get_active_function_name(), callback_name); + efree(callback_name); + return; + } + } + + array_init(return_value); + if (zend_hash_num_elements(Z_ARRVAL_PP(input)) == 0) + return; + + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(input), &pos); + zend_hash_get_current_data_ex(Z_ARRVAL_PP(input), (void **)&operand, &pos) == SUCCESS; + zend_hash_move_forward_ex(Z_ARRVAL_PP(input), &pos)) { + + if (callback) { + args[0] = operand; + if (call_user_function_ex(EG(function_table), NULL, *callback, &retval, 1, args, 0, NULL) == SUCCESS && retval) { + if (!zend_is_true(retval)) { + zval_ptr_dtor(&retval); + continue; + } else + zval_ptr_dtor(&retval); + } else { + php_error(E_WARNING, "%s() had an error invoking the reduction callback", get_active_function_name()); + return; + } + } else if (!zend_is_true(*operand)) + continue; + + zval_add_ref(operand); + switch (zend_hash_get_current_key_ex(Z_ARRVAL_PP(input), &string_key, &string_key_len, &num_key, 0, &pos)) { + case HASH_KEY_IS_STRING: + zend_hash_update(Z_ARRVAL_P(return_value), string_key, + string_key_len, operand, sizeof(zval *), NULL); + break; + + case HASH_KEY_IS_LONG: + zend_hash_index_update(Z_ARRVAL_P(return_value), num_key, + operand, sizeof(zval *), NULL); + break; + } + } +} +/* }}} */ + /* * Local variables: Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.316 php4/ext/standard/basic_functions.c:1.317 --- php4/ext/standard/basic_functions.c:1.316 Fri Mar 16 10:18:01 2001 +++ php4/ext/standard/basic_functions.c Fri Mar 16 12:46:33 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.316 2001/03/16 18:18:01 sniper Exp $ */ +/* $Id: basic_functions.c,v 1.317 2001/03/16 20:46:33 andrei Exp $ */ #include "php.h" #include "php_main.h" @@ -581,6 +581,7 @@ PHP_FE(array_intersect, NULL) PHP_FE(array_diff, NULL) PHP_FE(array_sum, NULL) + PHP_FE(array_filter, NULL) /* aliases from array.c */ PHP_FALIAS(pos, current, first_arg_force_ref) Index: php4/ext/standard/php_array.h diff -u php4/ext/standard/php_array.h:1.22 php4/ext/standard/php_array.h:1.23 --- php4/ext/standard/php_array.h:1.22 Sun Mar 11 19:06:53 2001 +++ php4/ext/standard/php_array.h Fri Mar 16 12:46:33 2001 @@ -19,7 +19,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_array.h,v 1.22 2001/03/12 03:06:53 andrei Exp $ */ +/* $Id: php_array.h,v 1.23 2001/03/16 20:46:33 andrei Exp $ */ #ifndef PHP_ARRAY_H #define PHP_ARRAY_H @@ -75,6 +75,7 @@ PHP_FUNCTION(array_intersect); PHP_FUNCTION(array_diff); PHP_FUNCTION(array_sum); +PHP_FUNCTION(array_filter); HashTable* php_splice(HashTable *, int, int, zval ***, int, HashTable **); PHPAPI void php_array_merge(HashTable *dest, HashTable *src, int recursive);

« previous php.cvs (#4619) next »