First to introduce the PHP in the array function basic knowledge warm up.
Definitions and usage
The In_array () function searches the array for a given value.
Grammar
In_array (Value,array,type)
Parameters |
Description |
Value |
Necessary. Specify the values to search for in the array. |
Array |
Necessary. Specify the array to search for. |
Type |
Optional. If this parameter is set to True, the search data is checked for the same type as the value of the array. |
Description
Returns true if the value given is present in array arrays. If the third argument is set to True, the function returns true only if the element exists in the array and the data type is the same as the given value.
If the argument is not found in the array, the function returns FALSE.
Note: If the value parameter is a string and the type argument is set to True, the search is case-sensitive.
Accidentally see a piece of code
<?php
$y = "1800";
$x = Array ();
For ($j =0 $j <50000; $j + +) {
$x []= ' {$j} ';
}
For ($i =0 $i <30000; $i + +) {
if (In_array ($y, $x)) {
continue
}}
Tested a bit
[Root@dev tmp]# time PHP b.php
Real 0m9.517s
User 0m4.486s
SYS 0m0.015s
I need 9s.
In_array is like this.
Copy Code code as follows:
BOOL In_array (mixed $needle, array $haystack [, bool $strict = FALSE])
Search for needle in haystack and use a relaxed comparison if strict is not set.
Needle
The value to search for. If the needle is a string, the comparison is case-sensitive.
Haystack
This array.
Strict
If the third argument strict a value of TRUE, the In_array () function also checks whether the needle type is the same as the haystack.
So let me take a look at the source code
The first step in the Ext/standard/array.c file
/*}}
//* {{proto bool In_array (mixed needle, array haystack [, BOOL strict])
Checks If the given value exist s in the array *
/php_function (In_array)
{
Php_search_array (internal_function_param_passthru, 0);
}
/*}}
//* {{proto mixed array_search (mixed needle, array haystack [, BOOL strict])
searches the array for a Given value and returns the corresponding key if successful * * php_function
(array_search)
{
Php_search_ Array (internal_function_param_passthru, 1);
}
/* }}} */
By the way see Array_search, originally and in_array internal realization basically consistent
Where the parameters of the function are in./zend.h
#define INTERNAL_FUNCTION_PARAM_PASSTHRU HT, Return_value, Return_value_ptr, this_ptr, return_value_used TSRMLS_CC
The second step is to view the Php_search_array prototype in the Ext/standard/array.c file
/* 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/* *array,/* Array to check in */**entry,/* Pointer to array entry/RES; * Comparison result * * Hashposition POS; /* Hash iterator * * Zend_bool strict = 0;
/* Strict comparison or not * * ULONG Num_key;
UINT Str_key_len;
Char *string_key;
Int (*is_equal_func) (Zval *, Zval *, Zval * tsrmls_dc) = is_equal_function;
if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "Za|b", &value, &array, &strict) = = FAILURE) {return;
} if (strict) {is_equal_func = is_identical_function;
} zend_hash_internal_pointer_reset_ex (Z_arrval_p (array), &pos); while (ZEND_HASH_GET_CURRENT_DATA_EX (z_arrval_p array), (void * *) &entry, &pos) = = SUCCESS) {Is_equal_func (&
Amp;res, value, *entry tsrmls_cc); if (Z_lval (res)) {if (behavior = = 0) {return_true; else {/* return current key/switch (ZEND_HASH_GET_CURRENT_KEY_EX z_arrval_p (array), &string_key, &st R_key_len, &num_key, 0, &pos) {case Hash_key_is_string:return_stringl (String_key, str_key_len-1, 1
);
Break
Case Hash_key_is_long:return_long (Num_key);
Break
}} zend_hash_move_forward_ex (Z_arrval_p (array), &pos);
} Return_false; }/* {{proto bool In_array (mixed needle, array haystack [, BOOL strict]) Checks if the given value exists I n the array * *
We found that there are two ways to compare strict this value, look at the difference between two functions
Is_identical_function Check whether the type is the same
Zend_api int is_identical_function (zval *result, Zval *op1, Zval *op2, TSRMLS_DC)/* {{* * * z_type_p (Result) = Is_boo
L
if (z_type_p (OP1)!= z_type_p (OP2)) {z_lval_p (result) = 0;
return SUCCESS;
Switch (z_type_p (OP1)) {case is_null:z_lval_p (result) = 1;
Break
Case Is_bool:case is_long:case is_resource:z_lval_p (Result) = (z_lval_p (OP1) = = Z_lval_p (OP2));
Break
Case is_double:z_lval_p (Result) = (z_dval_p (OP1) = = Z_dval_p (OP2));
Break Case is_string:z_lval_p (Result) = (Z_strlen_p (OP1) = = Z_strlen_p (OP2)) &&!memcmp (z_strval_p (OP1), Z_STRV
Al_p (OP2), Z_strlen_p (OP1)));
Break Case is_array:z_lval_p (Result) = (z_arrval_p (OP1) = = Z_arrval_p (OP2) zend_hash_compare (z_arrval_p (OP1), z_arrval_p (
OP2), (compare_func_t) hash_zval_identical_function, 1 tsrmls_cc) ==0);
Break Case Is_object:if (z_obj_ht_p (OP1) = = Z_obj_ht_p (OP2)) {z_lval_p (Result) = (z_obj_handle_p (OP1) = = Z_obj_handle_p (
OP2)); } else{z_lval_p (result) = 0;
} break;
Default:z_lval_p (Result) = 0;
return failure;
return SUCCESS; }
/* }}} */
Is_equal_function does not check whether the type is the same, so an implicit conversion is required
Zend_api int is_equal_function (zval *result, Zval *op1, Zval *op2)/* {{*/{if TSRMLS_DC (result,
OP1, op2 tsrmls_cc) = = failure) {return failure;
Zval_bool (result, (z_lval_p) = = 0));
return SUCCESS;
}/*} * * * = = compare_function Zend_api int compare_function (zval *result, Zval *op1, Zval *op2)/* {{* * *
int ret;
int converted = 0;
Zval op1_copy, op2_copy;
Zval *op_free; while (1) {switch (Type_pair (z_type_p (OP1), z_type_p (OP2))} {case Type_pair (Is_long, Is_long): Zval_long (Result,
Z_lval_p (OP1) >z_lval_p (OP2) 1: (Z_lval_p (OP1) <z_lval_p (OP2), -1:0));
return SUCCESS;
Case Type_pair (is_double, Is_long): z_dval_p (Result) = Z_dval_p (OP1)-(DOUBLE) z_lval_p (OP2);
Zval_long (Result, Zend_normalize_bool (z_dval_p));
return SUCCESS;
Case Type_pair (Is_long, is_double): z_dval_p (Result) = (DOUBLE) z_lval_p (OP1)-z_dval_p (OP2);
Zval_long (Result, Zend_normalize_bool (z_dval_p)); ReTurn SUCCESS;
Case Type_pair (is_double, is_double): if (z_dval_p (OP1) = = Z_dval_p (OP2)) {Zval_long (result, 0);
else {z_dval_p (Result) = Z_dval_p (OP1)-z_dval_p (OP2);
Zval_long (Result, Zend_normalize_bool (z_dval_p));
return SUCCESS;
Case Type_pair (Is_array, Is_array): Zend_compare_arrays (result, OP1, OP2 tsrmls_cc);
return SUCCESS;
Case Type_pair (Is_null, Is_null): Zval_long (result, 0);
return SUCCESS;
Case Type_pair (Is_null, Is_bool): Zval_long (Result, z_lval_p (OP2)? -1:0);
return SUCCESS;
Case Type_pair (Is_bool, Is_null): Zval_long (Result, z_lval_p (OP1)? 1:0);
return SUCCESS;
Case Type_pair (Is_bool, Is_bool): Zval_long (Result, Zend_normalize_bool (z_lval_p)-OP1 (z_lval_p));
return SUCCESS;
Case Type_pair (is_string, is_string): zendi_smart_strcmp (result, OP1, OP2);
return SUCCESS; Case Type_pair (Is_null, is_string): Zval_long (result, zend_binary_strcmp ("", 0, Z_strval_p (OP2), Z_strlen_p (OP2));
return SUCCESS;
Case Type_pair (is_string, Is_null): Zval_long (Result, zend_binary_strcmp (z_strval_p), OP1 (z_strlen_p), "", 0));
return SUCCESS;
Case Type_pair (Is_object, Is_null): Zval_long (result, 1);
return SUCCESS;
Case Type_pair (Is_null, Is_object): Zval_long (result,-1);
return SUCCESS; Case Type_pair (Is_object, Is_object):/* If both are objects sharing same comparision handler use is */if (Z_obj_handler_p (op1,compare_objects) = = Z_obj_handler_p (op2,compare_objects)) {if (z_obj_handle_p (OP1) = = Z_obj_handle_p (OP2)) {* * object handles are identical, apparently this is the same
Object */Zval_long (result, 0);
return SUCCESS;
Zval_long (Result, z_obj_ht_p (OP1)->compare_objects (OP1, OP2 tsrmls_cc));
return SUCCESS;
}/* Break missing intentionally * * DEFAULT:IF (z_type_p (OP1) = = Is_object) {if (z_obj_ht_p (OP1)->get) { Op_free = z_obj_ht_p (OP1)->gET (OP1 tsrmls_cc);
ret = compare_function (result, Op_free, OP2 tsrmls_cc);
Zend_free_obj_get_result (Op_free tsrmls_cc);
return ret;
else if (z_type_p (OP2)!= is_object && z_obj_ht_p (OP1)->cast_object) {alloc_init_zval (op_free);
if (z_obj_ht_p (OP1)->cast_object (OP1, Op_free, Z_type_p (OP2) tsrmls_cc) = = failure) {Zval_long (result, 1);
Zend_free_obj_get_result (Op_free tsrmls_cc);
return SUCCESS;
ret = compare_function (result, Op_free, OP2 tsrmls_cc);
Zend_free_obj_get_result (Op_free tsrmls_cc);
return ret; } if (Z_type_p (op2) = = Is_object) {if (z_obj_ht_p (OP2)->get) {Op_free = Z_obj_ht_p (OP2)->get (OP2
TSRMLS_CC);
ret = compare_function (result, OP1, Op_free tsrmls_cc);
Zend_free_obj_get_result (Op_free tsrmls_cc);
return ret;
else if (z_type_p (OP1)!= is_object && z_obj_ht_p (OP2)->cast_object) {alloc_init_zval (op_free); if (z_obj_ht_p(OP2)->cast_object (OP2, Op_free, Z_type_p (OP1) tsrmls_cc) = = failure) {Zval_long (result,-1);
Zend_free_obj_get_result (Op_free tsrmls_cc);
return SUCCESS;
ret = compare_function (result, OP1, Op_free tsrmls_cc);
Zend_free_obj_get_result (Op_free tsrmls_cc);
return ret;
else if (z_type_p (OP1) = = Is_object) {Zval_long (result, 1);
return SUCCESS;
} if (!converted) {if (z_type_p (OP1) = = Is_null) {Zendi_convert_to_boolean (OP2, op2_copy, result);
Zval_long (Result, z_lval_p (OP2)? -1:0);
return SUCCESS;
else if (z_type_p (op2) = = Is_null) {Zendi_convert_to_boolean (OP1, op1_copy, result);
Zval_long (Result, z_lval_p (OP1)? 1:0);
return SUCCESS;
else if (z_type_p (OP1) = = Is_bool) {Zendi_convert_to_boolean (OP2, op2_copy, result);
Zval_long (Result, Zend_normalize_bool (z_lval_p (OP1)-z_lval_p (OP2)));
return SUCCESS;
else if (z_type_p (op2) = = Is_bool) { Zendi_convert_to_boolean (OP1, op1_copy, result);
Zval_long (Result, Zend_normalize_bool (z_lval_p (OP1)-z_lval_p (OP2)));
return SUCCESS;
else {zendi_convert_scalar_to_number (OP1, op1_copy, result);
Zendi_convert_scalar_to_number (OP2, op2_copy, result);
converted = 1;
} else if (Z_type_p (OP1) ==is_array) {Zval_long (result, 1);
return SUCCESS;
else if (z_type_p (OP2) ==is_array) {Zval_long (result,-1);
return SUCCESS;
else if (z_type_p (OP1) ==is_object) {Zval_long (result, 1);
return SUCCESS;
else if (z_type_p (OP2) ==is_object) {Zval_long (result,-1);
return SUCCESS;
else {Zval_long (result, 0);
return failure; }
}
}
}
/* }}} */