]> granicus.if.org Git - php/commitdiff
added array_combine().
authorAndrey Hristov <andrey@php.net>
Mon, 13 Jan 2003 18:12:23 +0000 (18:12 +0000)
committerAndrey Hristov <andrey@php.net>
Mon, 13 Jan 2003 18:12:23 +0000 (18:12 +0000)
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.

ext/standard/array.c
ext/standard/basic_functions.c
ext/standard/php_array.h

index 774c820eda07a8bc4e5f604b3e0ae9bf15fc4065..74f372e84fe6dbf3a280c058cdda2dcb1954603f 100644 (file)
@@ -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
index 6704ee36107e49738494851e79f04e8ec47bd572..bd90f3b2879f7da5ebcffeb80c4faf6e33386e86 100644 (file)
@@ -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 */
index 36601f0f78f301b7f30044d3b19bef7582148881..3352e290b2458f3ecf9d5c8b4a5aea5a954773ee 100644 (file)
@@ -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);