cvs: /php4/ext/standard array.c php_array.h
| From: | Thies C. Arntzen | Date: | Sun, 21 Nov 1999 12:38:24 +0000 |
| Subject: | cvs: /php4/ext/standard array.c php_array.h | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-6619@lists.php.net to get a copy of this message | ||
thies Sun Nov 21 07:38:24 1999 EDT
Modified files:
/php4/ext/standard array.c php_array.h
Log:
(PHP array_flip) new function. no clash detection, only works for IS_STRING and IS_LONG datatypes
in src-array.
Index: php4/ext/standard/array.c
diff -u php4/ext/standard/array.c:1.7 php4/ext/standard/array.c:1.8
--- php4/ext/standard/array.c:1.7 Sun Nov 21 07:13:44 1999
+++ php4/ext/standard/array.c Sun Nov 21 07:37:53 1999
@@ -83,6 +83,7 @@
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)
@@ -1859,6 +1860,56 @@
/* Clean up */
efree(pads);
+}
+/* }}} */
+
+/* {{{ 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);
+ }
}
/* }}} */
Index: php4/ext/standard/php_array.h
diff -u php4/ext/standard/php_array.h:1.1 php4/ext/standard/php_array.h:1.2
--- php4/ext/standard/php_array.h:1.1 Sat Nov 13 15:31:54 1999
+++ php4/ext/standard/php_array.h Sun Nov 21 07:37:53 1999
@@ -27,7 +27,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_array.h,v 1.1 1999/11/13 20:31:54 andrei Exp $ */
+/* $Id: php_array.h,v 1.2 1999/11/21 12:37:53 thies Exp $ */
#ifndef _PHP_ARRAY_H
#define _PHP_ARRAY_H
@@ -75,6 +75,7 @@
PHP_FUNCTION(array_count_values);
PHP_FUNCTION(array_reverse);
PHP_FUNCTION(array_pad);
+PHP_FUNCTION(array_flip);
#define phpext_array_ptr array_module_ptr