cvs: php4 /ext/standard array.c basic_functions.c php_array.h
| From: | Edin Kadribasic | Date: | Fri, 05 Oct 2001 18:38:54 +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-7665@lists.php.net to get a copy of this message | ||
edink Fri Oct 5 14:38:54 2001 EDT
Modified files:
/php4/ext/standard array.c basic_functions.c php_array.h
Log:
Added array_change_key_case() function that returns array with
with all string keys lowercased or uppercased
@- Added array_change_key_case() function that returns array with
@ with all string keys lowercased or uppercased (EdinK)
Index: php4/ext/standard/array.c
diff -u php4/ext/standard/array.c:1.137 php4/ext/standard/array.c:1.138
--- php4/ext/standard/array.c:1.137 Mon Sep 17 17:02:51 2001
+++ php4/ext/standard/array.c Fri Oct 5 14:38:51 2001
@@ -21,7 +21,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: array.c,v 1.137 2001/09/17 21:02:51 sterling Exp $ */
+/* $Id: array.c,v 1.138 2001/10/05 18:38:51 edink Exp $ */
#include "php.h"
#include "php_ini.h"
@@ -65,6 +65,10 @@
#define SORT_DESC 3
#define SORT_ASC 4
+#define CASE_LOWER 0
+#define CASE_UPPER 1
+
+
PHP_MINIT_FUNCTION(array)
{
#ifdef ZTS
@@ -83,6 +87,8 @@
REGISTER_LONG_CONSTANT("SORT_REGULAR", SORT_REGULAR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SORT_NUMERIC", SORT_NUMERIC, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SORT_STRING", SORT_STRING, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("CASE_LOWER", CASE_LOWER, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("CASE_UPPER", CASE_UPPER, CONST_CS | CONST_PERSISTENT);
return SUCCESS;
}
@@ -2234,6 +2240,60 @@
}
zend_hash_move_forward_ex(target_hash, &pos);
+ }
+}
+/* }}} */
+
+/* {{{ proto array array_change_key_case(array input [, int case=CASE_LOWER])
+ Retuns an array with all string keys lowercased [or uppercased] */
+PHP_FUNCTION(array_change_key_case)
+{
+ zval **array, **entry, **to_upper;
+ char *string_key;
+ char *new_key;
+ uint str_key_len;
+ ulong num_key;
+ ulong change_to_upper=0;
+
+ HashPosition pos;
+
+ if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2 ||
+ zend_get_parameters_ex(ZEND_NUM_ARGS(), &array, &to_upper) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ if (ZEND_NUM_ARGS() > 1) {
+ convert_to_long_ex(to_upper);
+ change_to_upper = Z_LVAL_PP(to_upper);
+ }
+
+ if (Z_TYPE_PP(array) != IS_ARRAY) {
+ php_error(E_WARNING, "Wrong datatype in array_change_key_case() call");
+ RETURN_FALSE;
+ }
+
+ array_init(return_value);
+
+ zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(array), &pos);
+ while (zend_hash_get_current_data_ex(Z_ARRVAL_PP(array), (void **)&entry, &pos) ==
SUCCESS) {
+ (*entry)->refcount++;
+
+ switch (zend_hash_get_current_key_ex(Z_ARRVAL_PP(array), &string_key, &str_key_len,
&num_key, 0, &pos)) {
+ case HASH_KEY_IS_LONG:
+ zend_hash_index_update(Z_ARRVAL_P(return_value), num_key, entry, sizeof(entry), NULL);
+ break;
+ case HASH_KEY_IS_STRING:
+ new_key=estrndup(string_key,str_key_len);
+ if (change_to_upper)
+ php_strtoupper(new_key, str_key_len - 1);
+ else
+ php_strtolower(new_key, str_key_len - 1);
+ zend_hash_update(Z_ARRVAL_P(return_value), new_key, str_key_len, entry, sizeof(entry), NULL);
+ efree(new_key);
+ break;
+ }
+
+ zend_hash_move_forward_ex(Z_ARRVAL_PP(array), &pos);
}
}
/* }}} */
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.402 php4/ext/standard/basic_functions.c:1.403
--- php4/ext/standard/basic_functions.c:1.402 Tue Sep 25 17:58:22 2001
+++ php4/ext/standard/basic_functions.c Fri Oct 5 14:38:52 2001
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.c,v 1.402 2001/09/25 21:58:22 jeroen Exp $ */
+/* $Id: basic_functions.c,v 1.403 2001/10/05 18:38:52 edink Exp $ */
#include "php.h"
#include "php_main.h"
@@ -769,6 +769,7 @@
PHP_FE(array_reduce, NULL)
PHP_FE(array_pad, NULL)
PHP_FE(array_flip, NULL)
+ PHP_FE(array_change_key_case, NULL)
PHP_FE(array_rand, NULL)
PHP_FE(array_unique, NULL)
PHP_FE(array_intersect, NULL)
Index: php4/ext/standard/php_array.h
diff -u php4/ext/standard/php_array.h:1.28 php4/ext/standard/php_array.h:1.29
--- php4/ext/standard/php_array.h:1.28 Mon Sep 17 17:02:52 2001
+++ php4/ext/standard/php_array.h Fri Oct 5 14:38:53 2001
@@ -19,7 +19,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_array.h,v 1.28 2001/09/17 21:02:52 sterling Exp $ */
+/* $Id: php_array.h,v 1.29 2001/10/05 18:38:53 edink Exp $ */
#ifndef PHP_ARRAY_H
#define PHP_ARRAY_H
@@ -70,6 +70,7 @@
PHP_FUNCTION(array_reduce);
PHP_FUNCTION(array_pad);
PHP_FUNCTION(array_flip);
+PHP_FUNCTION(array_change_key_case);
PHP_FUNCTION(array_rand);
PHP_FUNCTION(array_unique);
PHP_FUNCTION(array_intersect);