PHP in Array,phparray into PHP kernel
First of all, we introduce the basic knowledge of PHP in array function hot warm-up.
Definition and usage
The In_array () function searches the array for the given value.
Grammar
In_array (Value,array,type)
parameter |
description |
value |
required. Specifies the value to search for in the array. |
array |
required. Specifies the array to search for. |
Type |
Optional. If set to true, checks whether the searched data is the same as the type of the array's value. |
Description
Returns true if the given value exists in an array of 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 no arguments are found in the array, the function returns FALSE.
Note: If the value parameter is a string and the type parameter is set to True, the search is case-sensitive.
Inadvertently 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 it.
[Root@dev tmp]# time PHP b.php
Real 0m9.517s
User 0m4.486s
SYS 0m0.015s
Need 9s
In_array is like this.
Copy the Code code as follows:
BOOL In_array (mixed $needle, array $haystack [, bool $strict = FALSE])
Search for needle in haystack, if not set strict then use a loose comparison.
Needle
The value to be searched. If needle is a string, the comparison is case-sensitive.
Haystack
This array.
Strict
If the value of the third parameter strict is TRUE then the In_array () function also checks whether the needle type is the same as in haystack.
Then I'll 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 exists 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, the original and In_array's internal implementation 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 */*arra Y,/* 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 ( &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, &str_ 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 in the Array */
We find that there are two different ways to compare the value of strict, and look at the differences between the two functions
Is_identical_function Check if the type is the same
Zend_api int is_identical_function (zval *result, Zval *op1, Zval *op2 tsrmls_dc)/* {{*/{z_type_p (result) = IS_BOOL; 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_strval _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 (op 2), (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 (op 2)); } 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 tsrmls_dc)/* {{*/{if (compare_function (result, OP1 , op2 tsrmls_cc) = = FAILURE) {return FAILURE;} Zval_bool (Result, (z_lval_p (result) = = 0)); return SUCCESS;} /*}}} */== "Compare_functionzend_api int compare_function (zval *result, Zval *op1, Zval *op2 tsrmls_dc)/* * {{*/{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)
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 tsrm LS_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; } } } } /* }}} */
http://www.bkjia.com/PHPjc/1069341.html www.bkjia.com true http://www.bkjia.com/PHPjc/1069341.html techarticle PHP in array,phparray into the PHP kernel first introduces the basic knowledge of the PHP in array function warm-up. Define and use the In_array () function to search the array for the given value. Grammar In_ ...