]> granicus.if.org Git - php/commitdiff
Added in_array() function.
authorAndrey Hristov <andrey@php.net>
Mon, 10 May 1999 21:10:48 +0000 (21:10 +0000)
committerAndrey Hristov <andrey@php.net>
Mon, 10 May 1999 21:10:48 +0000 (21:10 +0000)
ext/standard/basic_functions.c
ext/standard/basic_functions.h

index 0e0c2cb80e7ae055291921896d58a23dca1f0d68..cc87ababcb23aece21706b4be2016cf8209af45f 100644 (file)
@@ -294,6 +294,7 @@ function_entry basic_functions[] = {
        {"setcookie",           php3_SetCookie,         NULL},
        {"header",                      php3_Header,            NULL},
        PHP_FE(function_exists,                         NULL)
+       PHP_FE(in_array,                                        NULL)
        PHP_FE(extract,                                         NULL)
 
        {NULL, NULL, NULL}
@@ -2185,6 +2186,50 @@ PHP_FUNCTION(function_exists)
 /* }}} */
 
 
+/* {{{ proto bool in_array(array haystack, mixed needle)
+   Checks if the given value exists in the array */
+PHP_FUNCTION(in_array)
+{
+       zval *value,                            /* value to check for */
+                *array,                                /* array to check in */
+                **entry_ptr,                   /* pointer to array entry */
+                *entry,                                /* actual array entry */
+                 res;                                  /* comparison result */
+       HashTable *target_hash;         /* array hashtable */
+
+       if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &value, &array) == FAILURE) {
+               WRONG_PARAM_COUNT;
+       }
+       
+       if (value->type == IS_ARRAY || value->type == IS_OBJECT) {
+               zend_error(E_WARNING, "Wrong datatype for first argument in call to in_array()");
+               return;
+       }
+       
+       if (array->type != IS_ARRAY) {
+               zend_error(E_WARNING, "Wrong datatype for second argument in call to in_array()");
+               return;
+       }
+
+       target_hash = HASH_OF(array);
+       zend_hash_internal_pointer_reset(target_hash);
+       while(zend_hash_get_current_data(target_hash, (void **)&entry_ptr) == SUCCESS) {
+               entry = *entry_ptr;
+               if (entry->type == IS_STRING && entry->value.str.val == undefined_variable_string)
+                       continue;
+       is_equal_function(&res, value, entry);
+               if (zval_is_true(&res)) {
+                       RETURN_TRUE;
+               }
+               
+               zend_hash_move_forward(target_hash);
+       }
+       
+       RETURN_FALSE;
+}
+/* }}} */
+
+
 /* {{{ int _valid_var_name(char *varname) */
 static int _valid_var_name(char *varname)
 {
index 4eba444309fe59c10d6051508c4bfbfb9f2a46c8..675c7ccda07fa1ba897c6571ef5c5ef9d67df0f4 100644 (file)
@@ -119,6 +119,7 @@ PHP_FUNCTION(define);
 PHP_FUNCTION(defined);
 
 PHP_FUNCTION(function_exists);
+PHP_FUNCTION(in_array);
 PHP_FUNCTION(extract);
 
 #if HAVE_PUTENV