Exemplo n.º 1
0
 public function testIntersection()
 {
     // Using the default comparator.
     $array0 = CArray::fromElements("a", "b", "c", "d", "e", "f");
     $array1 = CArray::fromElements("g", "b", "h", "d", "i", "f");
     $array = CArray::intersection($array0, $array1);
     $this->assertTrue(CArray::equals($array, CArray::fromElements("b", "d", "f")));
     // 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::intersection($array0, $array1, $comparator);
     $this->assertTrue(CArray::equals($array, CArray::fromElements("b", "d", "f")));
 }
Exemplo n.º 2
0
 /**
  * Compares elements from one array with elements from another array and returns the common elements as a new
  * array.
  *
  * 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 $withArray The second 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 second array, and return `true` if the two elements are equal and `false` otherwise.
  *
  * @return CArrayObject The intersection array of the two arrays.
  *
  * @link   CComparator.html CComparator
  */
 public function intersection($withArray, $comparator = CComparator::EQUALITY)
 {
     return self::fromSplArray(CArray::intersection($this->m_splArray, $withArray, $comparator));
 }