public function testCountElement() { $array = CArray::fromElements("a", "c", "b", "c", "d", "e", "c", "c", "f", "g", "h", "c"); // Using the default comparator. $this->assertTrue(CArray::countElement($array, "c") == 5); // Using a custom comparator. $comparator = function ($string0, $string1) { return CString::toLowerCase($string0) === CString::toLowerCase($string1); }; $this->assertTrue(CArray::countElement($array, "C", $comparator) == 5); }
/** * Tells how many elements with a specified value there are in an array. * * You can use your own comparator for the search, but the default comparator has got you covered when searching * for 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 mixed $element The value of the searched element. * @param callable $comparator **OPTIONAL. Default is** `CComparator::EQUALITY`. The function or method to be * used for the comparison of element values while searching. If this parameter is provided, the comparator should * take two parameters, with the first parameter being an element from the array and the second parameter being the * searched element, and return `true` if the two elements are equal and `false` otherwise. * * @return int The number of such elements in the array. * * @link CComparator.html CComparator */ public function countElement($element, $comparator = CComparator::EQUALITY) { return CArray::countElement($this->m_splArray, $element, $comparator); }