PHP - the equivalent of an array_slice () object;

I am trying to remove the first two elements in an Object. For example, if I wanted to remove the first two elements from an array, I would use array_slice ($ arrayName, 2).

I tried this on my facility (hey, why not? I know this is not a technically array, but I am optimistic) and it did not work.

When searching for this, all I found were methods to remove elements from arrays.

    $categories = array_slice(Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*'), 2);


    foreach($categories as $category){
        echo "<div class='col'>{$category->getName()}</div>";
    }

In the above example, I would like to remove the first two categories from the object $categories(which are the “Root Category” and “Default” objects) before starting it through the foreach loop. What would be the best way to do this? I know what I can do;

if($category->getName() != 'Root Category' && $category->getName() != 'Default'){
  echo $category->getName();
}

But this seems like a dirty decision.

Edit

Patrick Q, , . , array_slice ? , array_slice .

2

. .

, ( ) , , . , , array_slice() . , , , , Magento . , , , .

+4
2

, , Magento.

()

PHP , . PHP , , .

, foreach count Magento, , PHP - IteratorAggregate Countable

#File: lib/Varien/Data/Collection.php
class Varien_Data_Collection implements IteratorAggregate, Countable
{
}

, ( Varien_Data_Collection ) foreach count().

Magento IteratorAggregate (, foreach) PHP, ArrayIterator class

#File: lib/Varien/Data/Collection.php
class Varien_Data_Collection implements IteratorAggregate, Countable
{
    public function getIterator()
    {
        $this->load();
        return new ArrayIterator($this->_items);
    }
}

, ArrayIterator, . - , , . , Magento PHP , - OO PHP.

, Magento, getArrayCopy .

$array = array_slice($categories->getIterator()->getArrayCopy(), 2);

(untested) PHP , .

, !

+4

LimitIterator - . :

$categories = new LimitIterator(
    Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->getIterator(), 
    2
);


foreach($categories as $category){
    echo "<div class='col'>{$category->getName()}</div>";
}
+3

All Articles