Browse Source

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)
PHP-4.0.5
Jason Greene 26 years ago
parent
commit
1888a98a4b
  1. 70
      ext/standard/array.c
  2. 1
      ext/standard/basic_functions.c
  3. 1
      ext/standard/php_array.h

70
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);
}
/* }}} */

1
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)

1
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);

Loading…
Cancel
Save