public function testSubarray() { $array = CArray::fromElements("a", "b", "c", "d", "e"); $subarray = CArray::subarray($array, 1, 4); $this->assertTrue(CArray::equals($subarray, CArray::fromElements("b", "c", "d"))); $subarray = CArray::subarray($array, 3, 5); $this->assertTrue(CArray::equals($subarray, CArray::fromElements("d", "e"))); // Special cases. $subarray = CArray::subarray($array, 5, 5); $this->assertTrue($subarray->getSize() == 0); $subarray = CArray::subarray($array, 0, 0); $this->assertTrue($subarray->getSize() == 0); $subarray = CArray::subarray($array, 2, 2); $this->assertTrue($subarray->getSize() == 0); }
/** * Returns a sequence of elements from an array, as another array, with both starting and ending positions * specified. * * This method works much like `substring` method of the CUStringObject class. * * The method allows for an empty array to be returned. * * @param int $startPos The position of the first element in the sequence to be copied. * @param int $endPos The position of the element that *follows* the last element in the sequence to be copied. * * @return CArrayObject The array containing the copied elements. */ public function subarray($startPos, $endPos) { return self::fromSplArray(CArray::subarray($this->m_splArray, $startPos, $endPos)); }