public function testDifference() { // Using the default comparator. $array0 = CArray::fromElements("a", "b", "c", "d", "e", "f"); $array1 = CArray::fromElements("g", "b", "h", "d", "i", "f"); $array = CArray::difference($array0, $array1); $this->assertTrue(CArray::equals($array, CArray::fromElements("a", "c", "e"))); // Using a custom comparator. $array0 = CArray::fromElements("a", "b", "c", "d", "e", "f"); $array1 = CArray::fromElements("G", "B", "H", "D", "I", "F"); $comparator = function ($string0, $string1) { return CString::toLowerCase($string0) === CString::toLowerCase($string1); }; $array = CArray::difference($array0, $array1, $comparator); $this->assertTrue(CArray::equals($array, CArray::fromElements("a", "c", "e"))); }
/** * From an array, removes only those elements that are present in another array and returns the new array. * * In the operation, *this* array is the minuend and the parameter array is the subtrahend. * * None of the source arrays is modified by this method. * * You can use your own comparator for the comparison of the elements in the arrays, but the default comparator has * got you covered when comparing scalar values, such as `string`, `int`, `float`, and `bool`. And the default * comparator is smart enough to know how to compare objects of those classes that conform to the IEquality or * IEqualityAndOrder interface (static or not), including CUStringObject, CArrayObject, CMapObject, CTime etc. See * the [CComparator](CComparator.html) class for more on this. * * @param array $subtrahendArray The subtrahend array. * @param callable $comparator **OPTIONAL. Default is** `CComparator::EQUALITY`. The function or method to be * used for the comparison of any two elements. If this parameter is provided, the comparator should take two * parameters, with the first parameter being an element from the minuend array and the second parameter being an * element from the subtrahend array, and return `true` if the two elements are equal and `false` otherwise. * * @return CArrayObject The difference array after subtracting the subtrahend array from the minuend array. * * @link CComparator.html CComparator */ public function difference($subtrahendArray, $comparator = CComparator::EQUALITY) { return self::fromSplArray(CArray::difference($this->m_splArray, $subtrahendArray, $comparator)); }