diff options
author | Thies C. Arntzen <thies@php.net> | 1999-09-20 10:09:57 +0000 |
---|---|---|
committer | Thies C. Arntzen <thies@php.net> | 1999-09-20 10:09:57 +0000 |
commit | 4bb329eafba6d1d8bc5148a43efbb611f5d672ca (patch) | |
tree | eec5bead432eb4e8b76fe09e23d0f4226895720c | |
parent | 22d6f64f54d5cc194ca49b9e80f6d5388509e394 (diff) | |
download | php-git-4bb329eafba6d1d8bc5148a43efbb611f5d672ca.tar.gz |
added array_count_values function.
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | ext/standard/basic_functions.c | 61 | ||||
-rw-r--r-- | ext/standard/basic_functions.h | 1 |
3 files changed, 63 insertions, 0 deletions
@@ -2,6 +2,7 @@ PHP 4.0 CHANGE LOG ChangeLog ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ?? 1999, Version 4.0 Beta 3 +- Added array_count_values() function. (Thies) - snmp, pgsql, mysql and gd modules can be built as dynamically loaded modules (Greg) - OCI8 fix for fetching empty LOBs (Thies) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 2f376468fc..e75f0f4ff4 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -337,6 +337,7 @@ function_entry basic_functions[] = { PHP_FE(array_merge, NULL) PHP_FE(array_keys, NULL) PHP_FE(array_values, NULL) + PHP_FE(array_count_values, NULL) PHP_FE(connection_aborted, NULL) PHP_FE(connection_timeout, NULL) @@ -3243,6 +3244,66 @@ PHP_FUNCTION(array_values) } /* }}} */ +/* {{{ proto array array_count_values(array input) + Return the value as key and the frequency of that value in <input> as value */ +PHP_FUNCTION(array_count_values) +{ + zval **input, /* Input array */ + **entry; /* An entry in the input array */ + zval **tmp; + HashTable *myht; + + /* Get arguments and do error-checking */ + if (ARG_COUNT(ht) != 1 || getParametersEx(1, &input) == FAILURE) { + WRONG_PARAM_COUNT; + } + + if ((*input)->type != IS_ARRAY) { + zend_error(E_WARNING, "Argument to array_count_values() should be an array"); + return; + } + + /* Initialize return array */ + array_init(return_value); + + /* Go through input array and add values to the return array */ + myht = (*input)->value.ht; + zend_hash_internal_pointer_reset(myht); + while (zend_hash_get_current_data(myht, (void **)&entry) == SUCCESS) { + if ((*entry)->type == IS_LONG) { + if (zend_hash_index_find(return_value->value.ht, + (*entry)->value.lval, + (void**)&tmp) == FAILURE) { + zval *data; + MAKE_STD_ZVAL(data); + data->type = IS_LONG; + data->value.lval = 1; + zend_hash_index_update(return_value->value.ht,(*entry)->value.lval, &data, sizeof(data), NULL); + } else { + (*tmp)->value.lval++; + } + } else if ((*entry)->type == IS_STRING) { + if (zend_hash_find(return_value->value.ht, + (*entry)->value.str.val, + (*entry)->value.str.len+1, + (void**)&tmp) == FAILURE) { + zval *data; + MAKE_STD_ZVAL(data); + data->type = IS_LONG; + data->value.lval = 1; + zend_hash_update(return_value->value.ht,(*entry)->value.str.val,(*entry)->value.str.len + 1, &data, sizeof(data), NULL); + } else { + (*tmp)->value.lval++; + } + } else { + zend_error(E_WARNING, "Can only count STRING and INTEGER values!"); + } + + zend_hash_move_forward(myht); + } +} +/* }}} */ + /* * Local variables: diff --git a/ext/standard/basic_functions.h b/ext/standard/basic_functions.h index cfd502e4de..d33a5559c8 100644 --- a/ext/standard/basic_functions.h +++ b/ext/standard/basic_functions.h @@ -141,6 +141,7 @@ PHP_FUNCTION(array_slice); PHP_FUNCTION(array_merge); PHP_FUNCTION(array_keys); PHP_FUNCTION(array_values); +PHP_FUNCTION(array_count_values); #if HAVE_PUTENV typedef struct { |