diff options
author | Andrei Zmievski <andrei@php.net> | 2001-03-16 20:46:33 +0000 |
---|---|---|
committer | Andrei Zmievski <andrei@php.net> | 2001-03-16 20:46:33 +0000 |
commit | 770c66125d176dbc2296af31b1b32d342d91917f (patch) | |
tree | e4be87a1020c78ca75ad2cb81bfad151941ee8a3 /ext | |
parent | f7f2cf9de58f88e0cd47264dcf29dfc5f399b4eb (diff) | |
download | php-git-770c66125d176dbc2296af31b1b32d342d91917f.tar.gz |
@- Added array_filter(), which allows filtering of array elements via
@ the specified callback. (Andrei)
Diffstat (limited to 'ext')
-rw-r--r-- | ext/standard/array.c | 76 | ||||
-rw-r--r-- | ext/standard/basic_functions.c | 1 | ||||
-rw-r--r-- | ext/standard/php_array.h | 1 |
3 files changed, 78 insertions, 0 deletions
diff --git a/ext/standard/array.c b/ext/standard/array.c index 6da53227fe..6b73339446 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -2887,6 +2887,82 @@ PHP_FUNCTION(array_reduce) *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: diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index d0c3b6d315..f8a1fca45a 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -581,6 +581,7 @@ function_entry basic_functions[] = { 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) diff --git a/ext/standard/php_array.h b/ext/standard/php_array.h index b38209d6f5..226574df92 100644 --- a/ext/standard/php_array.h +++ b/ext/standard/php_array.h @@ -75,6 +75,7 @@ PHP_FUNCTION(array_unique); 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); |