summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrey Hristov <andrey@php.net>2003-01-13 18:12:23 +0000
committerAndrey Hristov <andrey@php.net>2003-01-13 18:12:23 +0000
commit1cdc956637d6a328cd6c0f170aede2b5341b71c8 (patch)
tree23a662669b64917d1cf85823a517937d91da45b4
parentffeb57afb5ce6262f76592ef0da216239858c247 (diff)
downloadphp-git-1cdc956637d6a328cd6c0f170aede2b5341b71c8.tar.gz
added array_combine().
Creates an array by using the elements of the first parameter as keys and the elements of the second as correspoding keys. Error is thrown in case the arrays has different number of elements. Number of elements 0 is not valid for both parameters.
-rw-r--r--ext/standard/array.c42
-rw-r--r--ext/standard/basic_functions.c1
-rw-r--r--ext/standard/php_array.h1
3 files changed, 44 insertions, 0 deletions
diff --git a/ext/standard/array.c b/ext/standard/array.c
index 774c820eda..74f372e84f 100644
--- a/ext/standard/array.c
+++ b/ext/standard/array.c
@@ -3651,6 +3651,48 @@ PHP_FUNCTION(array_chunk)
}
/* }}} */
+/* {{{ proto array array_combine(array keys, array values)
+ Creates an array by using the elements of the first parameter as keys and the elements of the second as correspoding keys */
+PHP_FUNCTION(array_combine)
+{
+ zval *values, *keys;
+ HashPosition pos_values, pos_keys;
+ zval **entry_keys, **entry_values;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "aa", &keys, &values) == FAILURE) {
+ return;
+ }
+
+ if (zend_hash_num_elements(Z_ARRVAL_P(keys)) == 0 || zend_hash_num_elements(Z_ARRVAL_P(values)) == 0) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Both parameters should have number of elements at least 0");
+ RETURN_FALSE;
+ }
+
+
+ if (zend_hash_num_elements(Z_ARRVAL_P(keys)) != zend_hash_num_elements(Z_ARRVAL_P(values))) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Both parameters should have equal number of elements");
+ RETURN_FALSE;
+ }
+
+ array_init(return_value);
+
+ zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(keys), &pos_keys);
+ zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(values), &pos_values);
+ while (zend_hash_get_current_data_ex(Z_ARRVAL_P(keys), (void **)&entry_keys, &pos_keys) == SUCCESS &&
+ zend_hash_get_current_data_ex(Z_ARRVAL_P(values), (void **)&entry_values, &pos_values) == SUCCESS) {
+ if (Z_TYPE_PP(entry_keys) == IS_STRING) {
+ zval_add_ref(entry_values);
+ add_assoc_zval(return_value, Z_STRVAL_PP(entry_keys), *entry_values);
+ } else if (Z_TYPE_PP(entry_keys) == IS_LONG) {
+ zval_add_ref(entry_values);
+ add_index_zval(return_value, Z_LVAL_PP(entry_keys), *entry_values);
+ }
+ zend_hash_move_forward_ex(Z_ARRVAL_PP(entry_keys), &pos_keys);
+ zend_hash_move_forward_ex(Z_ARRVAL_PP(entry_values), &pos_values);
+ }
+}
+/* }}} */
+
/*
* Local variables:
* tab-width: 4
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index 6704ee3610..bd90f3b287 100644
--- a/ext/standard/basic_functions.c
+++ b/ext/standard/basic_functions.c
@@ -845,6 +845,7 @@ function_entry basic_functions[] = {
PHP_FE(array_filter, NULL)
PHP_FE(array_map, NULL)
PHP_FE(array_chunk, NULL)
+ PHP_FE(array_combine, NULL)
PHP_FE(array_key_exists, NULL)
/* aliases from array.c */
diff --git a/ext/standard/php_array.h b/ext/standard/php_array.h
index 36601f0f78..3352e290b2 100644
--- a/ext/standard/php_array.h
+++ b/ext/standard/php_array.h
@@ -83,6 +83,7 @@ PHP_FUNCTION(array_filter);
PHP_FUNCTION(array_map);
PHP_FUNCTION(array_key_exists);
PHP_FUNCTION(array_chunk);
+PHP_FUNCTION(array_combine);
HashTable* php_splice(HashTable *, int, int, zval ***, int, HashTable **);
PHPAPI int php_array_merge(HashTable *dest, HashTable *src, int recursive TSRMLS_DC);