public function testIsSubsetOf() { // Using the default comparator. $array = CArray::fromElements("a", "b", "c", "a", "b", "c", "a", "b", "c"); $this->assertTrue(CArray::isSubsetOf($array, CArray::fromElements("a", "b", "c"))); $array = CArray::fromElements("a", "b", "c", "a", "b", "c", "a", "d", "b", "c"); $this->assertFalse(CArray::isSubsetOf($array, CArray::fromElements("a", "b", "c"))); // Using a custom comparator. $array = CArray::fromElements("a", "b", "c", "a", "b", "c", "a", "b", "c"); $comparator = function ($string0, $string1) { return CString::toLowerCase($string0) === CString::toLowerCase($string1); }; $this->assertTrue(CArray::isSubsetOf($array, CArray::fromElements("A", "B", "C"), $comparator)); // Special case. $array = CArray::make(); $this->assertFalse(CArray::isSubsetOf($array, CArray::fromElements("a", "b", "c"))); }
/** * Determines if an array is a subset of another array. * * 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 $ofArray The reference 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 *this* array and the second parameter being an element * from the reference array, and return `true` if the two elements are equal and `false` otherwise. * * @return bool `true` if *this* array is a subset of the reference array, `false` otherwise. * * @link CComparator.html CComparator */ public function isSubsetOf($ofArray, $comparator = CComparator::EQUALITY) { return CArray::isSubsetOf($this->m_splArray, $ofArray, $comparator); }