This article mainly introduces about the PHP source code 18: About the ARRAY_DIFF_KEY,ARRAY_DIFF_ASSOC,ARRAY_UDIFF_ASSOC function, has a certain reference value, now share to everyone, the need for friends can refer to
Talking about PHP source code 18: About ARRAY_DIFF_KEY,ARRAY_DIFF_ASSOC,ARRAY_UDIFF_ASSOC function
"Array_diff_key"
(PHP 5 >= 5.1.0rc1)
array_diff_key-Calculating the difference set of an array using the key name comparison
Description
Array Array_diff_key (array array1, array array2 [, array ...])
Array_diff_key () returns an array that contains all the values that appear in the array1 but do not appear in any of the other parameter array's key names. Note that the association relationship remains unchanged.
"Array_diff_assoc"
(PHP 4 >= 4.3.0, PHP 5)
array_diff_assoc-with index Check the difference set of the computed array
Description
Array Array_diff_assoc (array array1, array array2 [, array ...])
ARRAY_DIFF_ASSOC () returns an array that contains all the values in array1 but not in any other parameter array. Note that unlike Array_diff (), the key name is also used for comparison.
The reason for putting these three functions together is that the last one they call is a function: Php_array_diff_key
"Array_diff_uassoc"
(PHP 5)
array_diff_uassoc-uses the user-supplied callback function to do an index check to calculate the difference set of the array
Description
Array Array_diff_uassoc (array array1, array array2 [, array ..., callback Key_compare_func])
ARRAY_DIFF_UASSOC () returns an array that contains all the values in array1 but not in any other parameter array.
These three functions call Php-array_diff_key in the following way:
Array_diff_key: php_array_diff_key (Internal_function_param_passthru, Diff_comp_data_none); ARRAY_DIFF_ASSOC: Php_array_diff_key (Internal_function_param_passthru, diff_comp_data_internal); Array_udiff_assoc: php_ Array_diff_key (Internal_function_param_passthru, Diff_comp_data_user);
The macro definition of the parameter is as follows:
#define INTERNAL_FUNCTION_PARAM_PASSTHRU HT, Return_value, Return_value_ptr, This_ptr, return_value_used TSRMLS_CC # Define Diff_comp_data_none -1#define diff_comp_data_internal 0#define diff_comp_data_user 1
The Php_array_diff_key function first determines the comparison function to be used, based on the incoming data_compare_type (three macros defined above), and then iterates through the first array, comparing each element to other arrays to see if other numbers exist
If present, delete the
Make an explanatory note about the source code for this function:
for (i = 0; i < argc; i++) {if (z_type_pp (args[i])! = Is_array) {php_error_docref (NULL tsrmls_cc, E_warning, "Argument #%d is not an array", I + 1); Retval_null (); Goto out; Goto statement, this seems to be a master with the general, hehe}} array_init (Return_value); Returns an array initialization that initializes the hash Table for the array to store (P = z_arrval_pp (args[0])->plisthead; p! = NULL; p = p->plistnext) {// Iterate through the first parameter contains the doubly linked list if (P->nkeylength = = 0) {//array index is numeric OK = 1; for (i = 1; i < argc; i++) {//The other array is judged if (Zend_hash_index_find (Z_ARRVAL_PP (Args[i]), P->h, (void**) &data) = = SUCCESS && (!diff_data_compare_func | | Diff_data_compare_func ((zval**) p->pdata, data tsrmls_cc) = = 0)) {OK = 0; Break }} if (OK) {in the first array, not in the other array (* ((zval**) p->pdata))->refcount++; Zend_hash_index_update (Z_arrval_p (Return_value), p->h, P->pdata, sizeof (zval*), NULL); }} else {//index = string ok = 1; for (i = 1; i < argc; i++) {if (Zend_hash_quick_find (Z_ARRVAL_PP (Args[i]), P->arkey, P->nkeylengt H, P->h, (void**) &data) = = SUCCESS && (!diff_data_compare_func | | Diff_data_compare_func ((zval**) p->pdata, data tsrmls_cc) = = 0)) {OK = 0; Break }} if (OK) {(* ((zval**) p->pdata))->refcount++; Zend_hash_quick_update (Z_arrval_p (Return_value), P->arkey, P->nkeylength, p->h, P->pData, sizeof (zval*) , NULL); } } }
About comparison functions:
The Diff_comp_data_none parameter corresponds to NULL, that is, there is no comparison, and using an index lookup is possible.
The diff_comp_data_internal parameter corresponds to the Zval_compare, that is, the index and the value are compared
The Diff_comp_data_user parameter corresponds to the Zval_user_compare, which is the user-defined function
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!