This article describes how to solve the problems encountered in the use of the PHP function array_udiff, which has some reference value. let's take a look at it.
Solve the problem encountered when using the PHP function array_udiff
Code = intval ($ code);} public function getCode () {return $ this-> code;}/*** this function must return 0, 1, -1 values can be used to calculate the travel set */public static function cmp ($ a, $ B) {if ($ a-> getCode () ===$ B-> getCode () return 0; return $ a-> getCode ()> $ B-> getCode ()? 1:-1;}/*** this form is written in the book "in-depth PHP object-oriented mode and practice". * It is verified to be invalid, in chapter 10, I will explain the combination mode */public static function cmp2 ($ a, $ B) {return $ a ===$ B? 0: 1 ;}}$ e1 = new Obj ('20140901'); $ e2 = new Obj ('20140901'); $ e3 = new Obj ('20140901 '); $ e4 = new Obj ('000000'); $ e5 = new Obj ('000000'); $ e6 = new Obj ('000000'); $ arr = [$ e1, $ e2, $ e3, $ e4, $ e5, $ e6]; $ arr2 = [$ e2, $ e3, $ e6]; $ arr = array_udiff ($ arr, $ arr2, ['obj ', 'cmp']); // $ arr = array_udiff ($ arr, $ arr2, ['obj ', 'cmp2']); // The comparison method cmp2 cannot return the correct result var_dump ($ arr );
Note:
1. The return result is a difference set, and the key name remains unchanged.
2. the two units are only in(String) $ elem1 ===( string) $ elem2Is considered the same. That is to say, when the expression of the string is the same.
Similar functions:
array_diff_assocarray_diff_uassocarray_diff_keyarray_diff_ukeyarray_udiff_assocarray_udiff_uassoc
Summary:
Array_diff and array_udiff are determined only by value.
Array_diff_key and array_diff_ukey are only determined based on the key.
Array_diff_assoc, array_diff_uassoc, array_udiff_assoc, and array_udiff_uassoc are determined by key and value together.
The above is the details about how to solve the problem encountered in the use of the PHP function array_udiff. For more information, see other related articles in the first PHP community!