diff options
author | Jason Greene <jason@php.net> | 2001-02-20 05:36:40 +0000 |
---|---|---|
committer | Jason Greene <jason@php.net> | 2001-02-20 05:36:40 +0000 |
commit | 1888a98a4b57e327e822ab6610739383aa320fbb (patch) | |
tree | f6a4523cf6bdf6218d17f45788c63399cdb45bc1 /ext | |
parent | fa8f552fbfa52c4583c43d224916e36f1bbaa44f (diff) | |
download | php-git-1888a98a4b57e327e822ab6610739383aa320fbb.tar.gz |
Moved the core of in_array into the function php_search_array, which is called by
in_array and search_array (new)
@ Added search_array which works similar to in_array but returns
@ the key instead of a boolean. (jason@php.net)
Diffstat (limited to 'ext')
-rw-r--r-- | ext/standard/array.c | 70 | ||||
-rw-r--r-- | ext/standard/basic_functions.c | 1 | ||||
-rw-r--r-- | ext/standard/php_array.h | 1 |
3 files changed, 61 insertions, 11 deletions
diff --git a/ext/standard/array.c b/ext/standard/array.c index 6d44cc4c25..7544813244 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -17,6 +17,7 @@ | Rasmus Lerdorf <rasmus@php.net> | | Andrei Zmievski <andrei@ispi.net> | | Stig Venaas <venaas@php.net> | + | Jason Greene <jason@php.net> | +----------------------------------------------------------------------+ */ @@ -1035,18 +1036,29 @@ PHP_FUNCTION(array_walk) { } /* }}} */ -/* {{{ proto bool in_array(mixed needle, array haystack [, bool strict]) - Checks if the given value exists in the array */ -PHP_FUNCTION(in_array) +/* void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior) + * 0 = return boolean + * 1 = return key + */ +static void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior) { - zval **value, /* value to check for */ + zval **value, /* value to check for */ **array, /* array to check in */ **strict, /* strict comparison or not */ **entry, /* pointer to array entry */ res; /* comparison result */ HashTable *target_hash; /* array hashtable */ HashPosition pos; /* hash iterator */ - int (*compare_func)(zval *, zval *, zval *) = is_equal_function; + ulong num_key; + char *string_key; + int (*compare_func)(zval *, zval *, zval *); + + if (behavior == 0) { + compare_func = is_equal_function; + } else { + /* Lets not return a key unless the values are exact */ + compare_func = is_identical_function; + } if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > 3 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &value, &array, &strict) == FAILURE) { @@ -1054,19 +1066,22 @@ PHP_FUNCTION(in_array) } if (Z_TYPE_PP(value) == IS_ARRAY || Z_TYPE_PP(value) == IS_OBJECT) { - php_error(E_WARNING, "Wrong datatype for first argument in call to in_array()"); + php_error(E_WARNING, "Wrong datatype for first argument in call to %s", get_active_function_name()); RETURN_FALSE; } if (Z_TYPE_PP(array) != IS_ARRAY) { - php_error(E_WARNING, "Wrong datatype for second argument in call to in_array()"); + php_error(E_WARNING, "Wrong datatype for second argument in call to %s", get_active_function_name()); RETURN_FALSE; } if (ZEND_NUM_ARGS() == 3) { convert_to_boolean_ex(strict); - if (Z_LVAL_PP(strict) == 1) + if (Z_LVAL_PP(strict)) { compare_func = is_identical_function; + } else { + compare_func = is_equal_function; + } } target_hash = HASH_OF(*array); @@ -1074,13 +1089,46 @@ PHP_FUNCTION(in_array) while(zend_hash_get_current_data_ex(target_hash, (void **)&entry, &pos) == SUCCESS) { compare_func(&res, *value, *entry); if (Z_LVAL(res) == 1) { - RETURN_TRUE; + if (behavior==0) { + RETURN_TRUE; + } else { + /* Return current key */ + switch (zend_hash_get_current_key_ex(target_hash, &string_key, NULL, &num_key, 1, &pos)) { + case HASH_KEY_IS_STRING: + RETVAL_STRING(string_key, 0); + break; + case HASH_KEY_IS_LONG: + RETVAL_LONG(num_key); + break; + } + } } zend_hash_move_forward_ex(target_hash, &pos); } - - RETURN_FALSE; + + if (behavior == 0) { + RETURN_FALSE; + } else { + return; + } + +} + + +/* {{{ proto bool in_array(mixed needle, array haystack [, bool strict]) + Checks if the given value exists in the array */ +PHP_FUNCTION(in_array) +{ + php_search_array(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); +} +/* }}} */ + +/* {{{ proto mixed search_array(mixed needle, array haystack [, bool strict]) + Searches the array for a given value and returns the corresponding key if successful */ +PHP_FUNCTION(search_array) +{ + php_search_array(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); } /* }}} */ diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 855457b815..55b3c0b994 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -550,6 +550,7 @@ function_entry basic_functions[] = { PHP_FE(min, NULL) PHP_FE(max, NULL) PHP_FE(in_array, NULL) + PHP_FE(search_array, NULL) PHP_FE(extract, NULL) PHP_FE(compact, NULL) PHP_FE(range, NULL) diff --git a/ext/standard/php_array.h b/ext/standard/php_array.h index 76d48ea330..0b2c9dcecc 100644 --- a/ext/standard/php_array.h +++ b/ext/standard/php_array.h @@ -49,6 +49,7 @@ PHP_FUNCTION(key); PHP_FUNCTION(min); PHP_FUNCTION(max); PHP_FUNCTION(in_array); +PHP_FUNCTION(search_array); PHP_FUNCTION(extract); PHP_FUNCTION(compact); PHP_FUNCTION(range); |