summaryrefslogtreecommitdiff
path: root/ext/standard/array.c
diff options
context:
space:
mode:
authorDavid Croft <david@php.net>2001-04-30 04:06:09 +0000
committerDavid Croft <david@php.net>2001-04-30 04:06:09 +0000
commit34f03f2c5cce5b668bfaa1ec04ef8d2f1e9c33a1 (patch)
treec5f50f148d4c91a74cdc77d070d4e729481a7278 /ext/standard/array.c
parentea7a6b407bcf54604579e3c41482bb90e2897ea9 (diff)
downloadphp-git-34f03f2c5cce5b668bfaa1ec04ef8d2f1e9c33a1.tar.gz
@ - Added key_exists() to check if a given key or index exists in an
@ array or object (David Croft) Added key_exists() to check if a given key or index exists in an array or object
Diffstat (limited to 'ext/standard/array.c')
-rw-r--r--ext/standard/array.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/ext/standard/array.c b/ext/standard/array.c
index 739dff9029..5c17c12f3e 100644
--- a/ext/standard/array.c
+++ b/ext/standard/array.c
@@ -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