Phpinarray in php Kernel

Source: Internet
Author: User
This article mainly introduces the phpinarray of php kernel. For more information, see

This article mainly introduces the related information about php in array in php kernel. For more information, see

First, we will introduce the basic knowledge of php in array functions to warm up.

Definition and usage

The in_array () function searches for the given value in the array.

Syntax
In_array (value, array, type)

Parameter description

Value is required. Specifies the value to be searched in the array.

Array is required. Specifies the array to be searched.

Type is optional. If this parameter is set to true, check whether the data to be searched is of the same type as the value of the array.

Description

Returns true if the given value exists in the array. If the third parameter is set to true, the function returns true only when the element exists in the array and the data type is the same as the given value.

If no parameter is 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 area is case sensitive.

Inadvertently seeing 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

[Root @ dev tmp] # time php B. php
Real 0m9. 517 s
User 0m4. 486 s
Sys 0m0. 015 s

It takes 9 s.

In_array looks like this

The Code is as follows:


Bool in_array (mixed $ needle, array $ haystack [, bool $ strict = FALSE])

Search for needle in haystack. If strict is not set, use loose comparison.

Needle

The value to be searched. If the needle is a string, the comparison is case sensitive.

Haystack

This array.

Strict

If the value of the third strict parameter is TRUE, the in_array () function checks whether the needle type is the same as that in haystack.

So let me take a look at the source code

The first step is 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, we can see that array_search is basically consistent with the internal implementation of in_array.

The function parameters are in./zend. h.

# Define INTERNAL_FUNCTION_PARAM_PASSTHRU ht, return_value, return_value_ptr, this_ptr, return_value_used TSRMLS_CC

Step 2 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_1__func) (zval *, zval *, zval * TSRMLS_DC) = is_1__function; if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "za | B", & value, & array, & strict) = FAILURE) {return;} if (strict) {is_assist_func = is_identical_function;} evaluate (Z_ARRVAL_P (array), & pos); while (TRIM (Z_ARRVAL_P (array), (void **) & entry, & pos) ==success) {is_effec_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 ;}} substring (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 found that there are two comparison methods for the value of strict. Let's take a look at the differences between the two functions.

Is_identical_function check whether the types are 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) = condition (op2) zend_hash_compare (Z_ARRVAL_P (op1), Z_ARRVAL_P (op2), (compare_func_t) condition, 1 TSRMLS_CC) = 0); break; case IS_OBJECT: if (Z_OBJ_HT_P (op1) = Z_OBJ_HT_P (op2) {Z_LVAL_P (result) = (partial (op1) = partial (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 types are the same, so implicit conversion is required.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.