andrey          Mon Jan 13 13:12:23 2003 EDT

  Modified files:              
    /php4/ext/standard  array.c basic_functions.c php_array.h 
  Log:
  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.
  
  
Index: php4/ext/standard/array.c
diff -u php4/ext/standard/array.c:1.214 php4/ext/standard/array.c:1.215
--- php4/ext/standard/array.c:1.214     Fri Jan  3 00:05:12 2003
+++ php4/ext/standard/array.c   Mon Jan 13 13:12:23 2003
@@ -21,7 +21,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: array.c,v 1.214 2003/01/03 05:05:12 moriyoshi Exp $ */
+/* $Id: array.c,v 1.215 2003/01/13 18:12:23 andrey Exp $ */
 
 #include "php.h"
 #include "php_ini.h"
@@ -3647,6 +3647,48 @@
        /* Add the final chunk if there is one. */
        if (chunk) {
                add_next_index_zval(return_value, 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);
        }
 }
 /* }}} */
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.558 
php4/ext/standard/basic_functions.c:1.559
--- php4/ext/standard/basic_functions.c:1.558   Sun Jan 12 09:47:17 2003
+++ php4/ext/standard/basic_functions.c Mon Jan 13 13:12:23 2003
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: basic_functions.c,v 1.558 2003/01/12 14:47:17 sebastian Exp $ */
+/* $Id: basic_functions.c,v 1.559 2003/01/13 18:12:23 andrey Exp $ */
 
 #include "php.h"
 #include "php_streams.h"
@@ -845,6 +845,7 @@
        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: php4/ext/standard/php_array.h
diff -u php4/ext/standard/php_array.h:1.40 php4/ext/standard/php_array.h:1.41
--- php4/ext/standard/php_array.h:1.40  Tue Dec 31 11:07:50 2002
+++ php4/ext/standard/php_array.h       Mon Jan 13 13:12:23 2003
@@ -19,7 +19,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: php_array.h,v 1.40 2002/12/31 16:07:50 sebastian Exp $ */
+/* $Id: php_array.h,v 1.41 2003/01/13 18:12:23 andrey Exp $ */
 
 #ifndef PHP_ARRAY_H
 #define PHP_ARRAY_H
@@ -83,6 +83,7 @@
 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);



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to