diff options
author | Thies C. Arntzen <thies@php.net> | 1999-11-21 12:37:53 +0000 |
---|---|---|
committer | Thies C. Arntzen <thies@php.net> | 1999-11-21 12:37:53 +0000 |
commit | 2451ff5368c222c96e3616ca38708b07d6364bc2 (patch) | |
tree | c38d503f5c93adf7b8e35fe2619a7c0977bd776b | |
parent | ee1380e6b02693cb09a4da6c1982290038d5bc1e (diff) | |
download | php-git-2451ff5368c222c96e3616ca38708b07d6364bc2.tar.gz |
@- Implemented array_flip() function. Returns input-array with key, value
@ flipped. (Thies)
(PHP array_flip) new function. no clash detection, only works for IS_STRING and IS_LONG datatypes in src-array.
-rw-r--r-- | ext/standard/array.c | 51 | ||||
-rw-r--r-- | ext/standard/php_array.h | 1 |
2 files changed, 52 insertions, 0 deletions
diff --git a/ext/standard/array.c b/ext/standard/array.c index d452607a9b..ac1d9e8d43 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -83,6 +83,7 @@ function_entry array_functions[] = { PHP_FE(array_count_values, NULL) PHP_FE(array_reverse, NULL) PHP_FE(array_pad, NULL) + PHP_FE(array_flip, NULL) /* Aliases */ PHP_FALIAS(pos, current, first_arg_force_ref) @@ -1862,6 +1863,56 @@ PHP_FUNCTION(array_pad) } /* }}} */ +/* {{{ proto array array_flip(array input) + Return array with key <-> value flipped. */ +PHP_FUNCTION(array_flip) +{ + zval **array, **entry, *data; + HashTable *target_hash; + char *string_key; + ulong num_key; + + if (ARG_COUNT(ht) != 1 || getParametersEx(1, &array) == FAILURE) { + WRONG_PARAM_COUNT; + } + + target_hash = HASH_OF(*array); + if (!target_hash) { + php_error(E_WARNING, "Wrong datatype in array_flip() call"); + RETURN_FALSE; + } + + array_init(return_value); + + zend_hash_internal_pointer_reset(target_hash); + while (zend_hash_get_current_data(target_hash, (void **)&entry) == SUCCESS) { + MAKE_STD_ZVAL(data); + switch (zend_hash_get_current_key(target_hash, &string_key, &num_key)) { + case HASH_KEY_IS_STRING: + data->value.str.val = string_key; + data->value.str.len = strlen(string_key); + data->type = IS_STRING; + break; + case HASH_KEY_IS_LONG: + data->type = IS_LONG; + data->value.lval = num_key; + break; + } + + if ((*entry)->type == IS_LONG) { + zend_hash_index_update(return_value->value.ht,(*entry)->value.lval, &data, sizeof(data), NULL); + } else if ((*entry)->type == IS_STRING) { + zend_hash_update(return_value->value.ht,(*entry)->value.str.val,(*entry)->value.str.len + 1, &data, sizeof(data), NULL); + } else { + zval_dtor(data); + php_error(E_WARNING, "Can only flip STRING and INTEGER values!"); + } + + zend_hash_move_forward(target_hash); + } +} +/* }}} */ + int multisort_compare(const void *a, const void *b) { Bucket** ab = *(Bucket ***)a; diff --git a/ext/standard/php_array.h b/ext/standard/php_array.h index 312c90fcbb..b42eaed6b1 100644 --- a/ext/standard/php_array.h +++ b/ext/standard/php_array.h @@ -75,6 +75,7 @@ PHP_FUNCTION(array_values); PHP_FUNCTION(array_count_values); PHP_FUNCTION(array_reverse); PHP_FUNCTION(array_pad); +PHP_FUNCTION(array_flip); #define phpext_array_ptr array_module_ptr |