1 php_function (Array_diff)2 {3Zval *args;4 intargc, I;5 uint32_t num;6 HashTable exclude;7Zval *value;8Zend_string *str, *key;9 Zend_long idx;Ten Zval Dummy; One A //minimum of two parameters - if(Zend_num_args () <2) { -Php_error_docref (NULL, e_warning,"at least 2 parameters is required,%d given", Zend_num_args ()); the return; - } - //Type descriptor: * Variable arguments list (0 or more) - //type descriptor: + variable arguments list (1 or more) + //parameters are stored in the array args, ARGC save the number of arguments - if(Zend_parse_parameters (Zend_num_args (),"+", &args, &argc) = =FAILURE) { + return; A } at - //The first argument is not an array, then an error - if(Z_type (args[0]) !=Is_array) { -Php_error_docref (NULL, e_warning,"Argument #1 is isn't an array"); -Return_null ();//returns null - } in - /*count Number of elements*/ to //checks if all parameters are arrays +num =0; - for(i =1; i < argc; i++) { the if(Z_type (args[i])! =Is_array) { *Php_error_docref (NULL, e_warning,"Argument #%d isn't an array", i +1); $Return_null ();//returns nullPanax Notoginseng } -num + = zend_hash_num_elements (Z_arrval (args[i));//cumulative number of elements in all parameter arrays the } + A //number of elements num is 0, returns the first array of arguments (empty array) the if(num = =0) { +Zval_copy (Return_value, &args[0]); - return; $ } $ -Zval_null (&dummy); - /*Create exclude Map*/ theZend_hash_init (&exclude, num, null, NULL,0); - //start with the second array, so the array elements are saved to excludeWuyi for(i =1; i < argc; i++) { the //iterating through an array - Zend_hash_foreach_val_ind (Z_arrval (Args[i]), value) { Wustr = zval_get_string (value);//array element value converted to string -Zend_hash_add (&exclude, str, &dummy);// About zend_string_release (str); $ } zend_hash_foreach_end (); - } - - /*Copy all elements of the first array that is not in exclude set*/ A //Initializes an array of return values with the same size as the first array. +Array_init_size (Return_value, Zend_hash_num_elements (Z_arrval (args[0]))); the //iterate through the first array -Zend_hash_foreach_key_val_ind (Z_arrval (args[0]), IDX, key, value) { $str = zval_get_string (value);//element value to string the if(!zend_hash_exists (&exclude, str)) {//the value could not be found in exclude the //inserted into the return value array (the difference set), the key name remains unchanged. the if(key) { theValue =zend_hash_add_new (z_arrval_p (return_value), key, value); -}Else { inValue =zend_hash_index_add_new (z_arrval_p (return_value), idx, value); the } the zval_add_ref (value); About } the zend_string_release (str); the } zend_hash_foreach_end (); the +Zend_hash_destroy (&exclude); -}
PHP built-in function Analysis Array_diff ()