]> granicus.if.org Git - php/commitdiff
Don't set is_ref in _phpi_splice()
authorAndrey Hristov <andrey@php.net>
Mon, 7 Jun 1999 18:52:20 +0000 (18:52 +0000)
committerAndrey Hristov <andrey@php.net>
Mon, 7 Jun 1999 18:52:20 +0000 (18:52 +0000)
More checking in array_merge()
Added keys() and values() array functions.

ext/standard/basic_functions.c
ext/standard/basic_functions.h

index 0fb1571edb942d6a5dd62cb872e65d544e3cb3b0..596225f3dc25884db72845575d414e54885c664e 100644 (file)
@@ -304,6 +304,8 @@ function_entry basic_functions[] = {
        PHP_FE(splice,                                          first_arg_force_ref)
        PHP_FE(slice,                                           NULL)
        PHP_FE(array_merge,                                     NULL)
+       PHP_FE(keys,                                            NULL)
+       PHP_FE(values,                                          NULL)
 
        {NULL, NULL, NULL}
 };
@@ -2497,7 +2499,6 @@ HashTable* _phpi_splice(HashTable *in_hash, int offset, int length,
                   and copy it into the output hash */
                for (i=0; i<list_count; i++) {
                        entry = list[i];
-                       entry->is_ref = 1;
                        entry->refcount++;
                        zend_hash_next_index_insert(out_hash, &entry, sizeof(zval *), NULL);
                }
@@ -2855,7 +2856,10 @@ PHP_FUNCTION(array_merge)
        array_init(return_value);
        
        for (i=0; i<argc; i++) {
-               convert_to_array(args[i]);
+               if (args[i]->type != IS_ARRAY) {
+                       zend_error(E_WARNING, "Skipping argument #%d to array_merge(), since it's not an array", i+1);
+                       continue;
+               }
                hash = args[i]->value.ht;
                
                zend_hash_internal_pointer_reset(hash);
@@ -2884,6 +2888,93 @@ PHP_FUNCTION(array_merge)
 /* }}} */
 
 
+/* {{{ proto array keys(array input)
+   Return just the keys from the input array */
+PHP_FUNCTION(keys)
+{
+       zval            *input,                 /* Input array */
+                          **entry,                     /* An entry in the input array */
+                               *new_val;               /* New value */
+       char            *string_key;    /* String key */
+       ulong            num_key;               /* Numeric key */
+       
+       /* Get arguments and do error-checking */
+       if (ARG_COUNT(ht) != 1 || getParameters(ht, ARG_COUNT(ht), &input) == FAILURE) {
+               WRONG_PARAM_COUNT;
+       }
+       
+       if (input->type != IS_ARRAY) {
+               zend_error(E_WARNING, "Argument to keys() should be an array");
+               return;
+       }
+       
+       /* Initialize return array */
+       array_init(return_value);
+       
+       /* Go through input array and add keys to the return array */
+       zend_hash_internal_pointer_reset(input->value.ht);
+       while(zend_hash_get_current_data(input->value.ht, (void **)&entry) == SUCCESS) {
+               new_val = (zval *)emalloc(sizeof(zval));
+               new_val->is_ref = 0;
+               new_val->refcount = 1;
+               
+               switch (zend_hash_get_current_key(input->value.ht, &string_key, &num_key)) {
+                       case HASH_KEY_IS_STRING:
+                               new_val->type = IS_STRING;
+                               new_val->value.str.val = string_key;
+                               new_val->value.str.len = strlen(string_key);
+                               zend_hash_next_index_insert(return_value->value.ht, &new_val,
+                                                                                       sizeof(zval *), NULL);
+                               break;
+
+                       case HASH_KEY_IS_LONG:
+                               new_val->type = IS_LONG;
+                               new_val->value.lval = num_key;
+                               zend_hash_next_index_insert(return_value->value.ht, &new_val,
+                                                                                       sizeof(zval *), NULL);
+                               break;
+               }
+
+               zend_hash_move_forward(input->value.ht);
+       }
+}
+/* }}} */
+
+
+/* {{{ proto array values(array input)
+   Return just the values from the input array */
+PHP_FUNCTION(values)
+{
+       zval            *input,         /* Input array */
+                          **entry;             /* An entry in the input array */
+       
+       /* Get arguments and do error-checking */
+       if (ARG_COUNT(ht) != 1 || getParameters(ht, ARG_COUNT(ht), &input) == FAILURE) {
+               WRONG_PARAM_COUNT;
+       }
+       
+       if (input->type != IS_ARRAY) {
+               zend_error(E_WARNING, "Argument to values() should be an array");
+               return;
+       }
+       
+       /* Initialize return array */
+       array_init(return_value);
+
+       /* Go through input array and add values to the return array */ 
+       zend_hash_internal_pointer_reset(input->value.ht);
+       while(zend_hash_get_current_data(input->value.ht, (void **)&entry) == SUCCESS) {
+               
+               (*entry)->refcount++;
+               zend_hash_next_index_insert(return_value->value.ht, entry,
+                                                                                       sizeof(zval *), NULL);
+
+               zend_hash_move_forward(input->value.ht);
+       }
+}
+/* }}} */
+
+
 /*
  * Local variables:
  * tab-width: 4
index e3f70559af661fa068ccfb6659eacc1160d604c2..35c038d0b350d3d4c15f6a3fd62e1cde5a2b4a1d 100644 (file)
@@ -129,6 +129,8 @@ PHP_FUNCTION(unshift);
 PHP_FUNCTION(splice);
 PHP_FUNCTION(slice);
 PHP_FUNCTION(array_merge);
+PHP_FUNCTION(keys);
+PHP_FUNCTION(values);
 
 #if HAVE_PUTENV
 typedef struct {