PHP - Object equivalent of array_slice();

PHP - Object equivalent of array_slice();

July 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
PHP - Object equivalent of array_slice();

PHP - Object equivalent of array_slice()

You’re right on track thinking that there should be an object-oriented way to handle this situation, similar to how we use array_slice() for arrays. In PHP, objects are more complex than simple arrays, but you can certainly achieve the same result using built-in methods and some clever programming.

Firstly, let’s clarify the nature of your $categories variable. You mentioned it’s an object, but the code snippet suggests it could be a collection of objects (multiple instances of Mage_Catalog_Model_Category). To confirm this, you can use the is_array() function:

if(is_array($categories)) {
    echo "It's an array.";
} else if(is_object($categories)) {
    echo "It's an object.";
}

Based on the provided code and PHP standards, $categories is likely a collection of objects wrapped in an array-like structure, which can happen with Magento collections.

Now, to remove the first two elements from this collection:

  1. Using iteration and conditional removal:
$newCategories = array();
foreach($categories as $key => $category) {
    if ($key > 1) { // Skip index 0 and 1 (which are the first two categories)
        $newCategories[] = $category;
    }
}

In this example, we create a new array $newCategories and only add elements to it when their index is greater than 1. This effectively removes the first two items.

  1. Using array_map() with conditional filtering:
$filteredCategories = array_map(function($category) {
    return ($category['name'] != 'Root Category' && $category['name'] != 'Default') ? $category : null;
}, $categories);

This method uses array_map to apply a custom function to each element in the array. The function checks if the category name is either ‘Root Category’ or ‘Default’, and returns null for those categories, effectively removing them.

  1. Using array_filter() with conditional filtering:
$filteredCategories = array_filter($categories, function($category) {
    return ($category['name'] != 'Root Category' && $category['name'] != 'Default');
});

This is similar to the previous example but uses array_filter instead. The second argument is a callback function that determines whether an element should be included (true) or excluded (false).

Remember, null values returned by array_map will still show up in the result array if you don’t handle them properly.

Once you have your filtered collection $newCategories or $filteredCategories, you can iterate through it as usual:

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

Don’t forget to replace name with the actual attribute name used in your objects.

In conclusion, while PHP doesn’t have a built-in method like array_slice() for objects, you can achieve similar results using basic array functions and conditional logic. Always pay attention to the nature of your data (arrays vs. objects vs. collections) when choosing the appropriate approach.

For more advanced object handling in Laravel applications, consider checking out our guide on collections.

Catalog Indexing

Our network