From: Andrey Hristov Date: Mon, 10 May 1999 21:10:48 +0000 (+0000) Subject: Added in_array() function. X-Git-Tag: BEFORE_PHP4_APACHE_MODULE_CHANGE~71 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d7ed9c14559c4c84788630b1fb74ccb483e7ee03;p=php Added in_array() function. --- diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 0e0c2cb80e..cc87ababcb 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -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) { diff --git a/ext/standard/basic_functions.h b/ext/standard/basic_functions.h index 4eba444309..675c7ccda0 100644 --- a/ext/standard/basic_functions.h +++ b/ext/standard/basic_functions.h @@ -119,6 +119,7 @@ PHP_FUNCTION(define); PHP_FUNCTION(defined); PHP_FUNCTION(function_exists); +PHP_FUNCTION(in_array); PHP_FUNCTION(extract); #if HAVE_PUTENV