]> granicus.if.org Git - php/commitdiff
Motivated by bug #13607 I wrote up a simple array_init() function that
authorRasmus Lerdorf <rasmus@php.net>
Sun, 21 Oct 2001 07:42:35 +0000 (07:42 +0000)
committerRasmus Lerdorf <rasmus@php.net>
Sun, 21 Oct 2001 07:42:35 +0000 (07:42 +0000)
lets you quickly create an array and initialize each element to a certain
value.
@ Add array_init() function (Rasmus)

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

index 0630a39ab0dd8526894b8957c9700f17b8f2a978..f588a4f0efc6e67352a005313e792343cb97b313 100644 (file)
@@ -516,7 +516,7 @@ PHP_FUNCTION(rsort)
        }
        RETURN_TRUE;
 }
-
+/* }}} */
 
 static int array_user_compare(const void *a, const void *b TSRMLS_DC)
 {
@@ -1325,6 +1325,55 @@ PHP_FUNCTION(compact)
 }
 /* }}} */
 
+/* {{{ proto array array_init(int start_key, int num, mixed val)
+   Create an array containing num elements starting with index start_key each initialized to val */
+PHP_FUNCTION(array_init)
+{
+       zval **start_key, **num, **val, *newval;
+       int i;
+
+       if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &start_key, &num, &val) == FAILURE) {
+               WRONG_PARAM_COUNT;
+       }
+
+    /* allocate an array for return */
+    if (array_init(return_value) == FAILURE) {
+               RETURN_FALSE;
+    }
+
+       SEPARATE_ZVAL(val);
+
+       switch(Z_TYPE_PP(start_key)) {
+               case IS_STRING:
+               case IS_LONG:
+               case IS_DOUBLE:
+                       convert_to_long_ex(start_key);
+                       MAKE_STD_ZVAL(newval);
+                       *newval = **val;
+                       zval_copy_ctor(newval);
+                       add_index_zval(return_value, Z_LVAL_PP(start_key), newval);
+                       break;
+               default:
+                       php_error(E_WARNING, "Wrong datatype for start key in array_init()");
+                       RETURN_FALSE;
+                       break;
+       }       
+
+       convert_to_long_ex(num);
+       i = Z_LVAL_PP(num) - 1; 
+       if(i<0) {
+               php_error(E_WARNING, "Number of elements must be positive in array_init()");
+               RETURN_FALSE;
+       }
+       while(i--) {
+               MAKE_STD_ZVAL(newval);
+               *newval = **val;
+               zval_copy_ctor(newval);
+               add_next_index_zval(return_value, newval);
+       }
+}
+/* }}} */
+
 /* {{{ proto array range(mixed low, mixed high)
    Create an array containing the range of integers or characters from low to high (inclusive) */
 PHP_FUNCTION(range)
index 056c2e01206fac0fd04f09e79fc7ada1adb79d4a..a8a630f9721cb1d85a9c907eb9430cb5d3f55f91 100644 (file)
@@ -755,6 +755,7 @@ function_entry basic_functions[] = {
        PHP_FE(array_search,                                                                                                    NULL)
        PHP_FE(extract,                                                                                                                 NULL)
        PHP_FE(compact,                                                                                                                 NULL)
+       PHP_FE(array_init,                                                                                                              NULL)
        PHP_FE(range,                                                                                                                   NULL)
        PHP_FE(array_multisort,                                                                                                 NULL)
        PHP_FE(array_push,                              first_arg_force_ref)
index fde51f4dbeb1f0fc30544b34d150c8631a4cc646..7598f2aaa526a4e3de41d7e6ad1905112377136d 100644 (file)
@@ -52,6 +52,7 @@ PHP_FUNCTION(in_array);
 PHP_FUNCTION(array_search);
 PHP_FUNCTION(extract);
 PHP_FUNCTION(compact);
+PHP_FUNCTION(array_init);
 PHP_FUNCTION(range);
 PHP_FUNCTION(shuffle);
 PHP_FUNCTION(array_multisort);