]> granicus.if.org Git - php/commitdiff
@ - Added key_exists() to check if a given key or index exists in an
authorDavid Croft <david@php.net>
Mon, 30 Apr 2001 04:06:09 +0000 (04:06 +0000)
committerDavid Croft <david@php.net>
Mon, 30 Apr 2001 04:06:09 +0000 (04:06 +0000)
@ array or object (David Croft)
Added key_exists() to check if a given key or index exists in an array or object

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

index 739dff90299c695a4ce0085f428ee9aac0bf5547..5c17c12f3e96944555a2c52e9f2e3fdc45872894 100644 (file)
@@ -3075,6 +3075,44 @@ PHP_FUNCTION(array_map)
 /* }}} */
 
 
+/* {{{ proto bool key_exists(mixed key, array search)
+   Checks if the given key or index exists in the array */
+PHP_FUNCTION(key_exists)
+{
+       zval **key,                                     /* key to check for */
+                **array;                               /* array to check in */
+       
+       if (ZEND_NUM_ARGS() != 2 ||
+               zend_get_parameters_ex(ZEND_NUM_ARGS(), &key, &array) == FAILURE) {
+               WRONG_PARAM_COUNT;
+       }
+       
+       if (Z_TYPE_PP(array) != IS_ARRAY && Z_TYPE_PP(array) != IS_OBJECT) {
+               php_error(E_WARNING, "Wrong datatype for second argument in call to %s", get_active_function_name());
+               RETURN_FALSE;
+       }
+
+       switch (Z_TYPE_PP(key)) {
+               case IS_STRING:
+                       if (zend_hash_exists(HASH_OF(*array), Z_STRVAL_PP(key), Z_STRLEN_PP(key)+1)) {
+                               RETURN_TRUE;
+                       }
+                       RETURN_FALSE;
+
+               case IS_LONG:
+                       if (zend_hash_index_exists(HASH_OF(*array), Z_LVAL_PP(key))) {
+                               RETURN_TRUE;
+                       }
+                       RETURN_FALSE;
+
+               default:
+                       php_error(E_WARNING, "Wrong datatype for first argument in call to %s", get_active_function_name());
+                       RETURN_FALSE;
+       }
+                       
+}
+/* }}} */
+
 /*
  * Local variables:
  * tab-width: 4
index 61e7f8286956026084c7e5694ce7e6087fef9765..7e779a7f7503fb0a358b087cdac1004b89ca5355 100644 (file)
@@ -588,6 +588,7 @@ function_entry basic_functions[] = {
        PHP_FE(array_sum,                                                           NULL)
        PHP_FE(array_filter,                                                NULL)
        PHP_FE(array_map,                                                           NULL)
+       PHP_FE(key_exists,                                                              NULL)
 
        /* aliases from array.c */
        PHP_FALIAS(pos,                         current,                        first_arg_force_ref)
index fb00e2d08ab226e4b1daf55af0b3fb6bd2e4e074..04ebb0216766df4df1e25726203c239e9ef8967d 100644 (file)
@@ -77,6 +77,7 @@ PHP_FUNCTION(array_diff);
 PHP_FUNCTION(array_sum);
 PHP_FUNCTION(array_filter);
 PHP_FUNCTION(array_map);
+PHP_FUNCTION(key_exists);
 
 HashTable* php_splice(HashTable *, int, int, zval ***, int, HashTable **);
 PHPAPI void php_array_merge(HashTable *dest, HashTable *src, int recursive);