Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:39:22+00:00 2026-06-05T11:39:22+00:00

Hi I need some iterator advice. I have a Category object which can contain

  • 0

Hi I need some iterator advice.

I have a Category object which can contain a collection of items and also can have children categories.

From doctrine ORM I do get a collection of category objects. Now I want to traverse this collection and flatten the category tree structure. So the children categories are at the same level as it’s parent. I would also like to filter the children.

Maybe somebody can point me in the right direction, currently a bit lost in the Iterator cloud.

<?php

class Category
{
    private $name;

    private $children;

    private $type;

    private $parent;

    private $items;

    //parent category
    public function getParent()
    {
        return $this->parent;
    }

    public function setItems($items)
    {
        $this->items = $items;
    }


    public function getItems()
    {
        return $this->items;
    }

    //colelction of categories
    public function getChildren()
    {
        return $this->children;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:39:23+00:00Added an answer on June 5, 2026 at 11:39 am

    You simply need to implement the RecursiveIterator interface. Then, you can iterate over it using the concrete RecursiveIteratorIterator.

    To help you understand…

    A RecursiveIterator isn’t very, um, “recursive” by itself. It is just something that offers up certain methods that can be used to get the children(the subproblem in recursion can be thought of as “children”). Notice that RecursiveIterator.getChildren() must return its children in the form of another RecursiveIterator.

    You could manually iterate a plain RecursiveIterator, but, it would be pretty painful to keep track of all the sub iterators returned by the recursive calls to getChildren, and maintain the proper depth etc…thats where RecursiveIteratorIterator comes in…

    A RecursiveIteratorIterator is the thing that does the actual work to systematically traverse the structure, mimicking recursion. It iterates through a RecursiveIterator as if it were a flat list, but at each element in the list, it tests the current element for the presence of children. If it hasChildren, it calls getChildren and stores the reference to this new child iterator in a Stack. It manages the Stack in a fashion that provides the recursive behavior you expect(much the same way you manually transform a recursive function into an iterative version).

    To be clear, you don’t code your own RecursiveIteratorIterator, just instantiate php’s concrete implementation. This class exists purely to hide the complications and manage all the many RecursiveIterator objects that get instantiated in the traversal process from you, and presenting the result of the traversal to you as what seems like a flat list. RecursiveIteratorIterator is a very complicated class internally.

    As for filtering-

    There’s a few ways. For ease of use, I recommend to use CallbackFilterIterator if you have php 5.4. Otherwise, you must extend FilterIterator.

    However, both of those filter the elements out after the, um, view of the recursive structure has been flattened into a list like structure. So, your filter cannot for example say “skip this entire subtree”, it can only say “skip this single element”. If you need to say “skip this entire subtree” you need to use RecursiveCallbackFilterIterator or extend RecursiveFilterIterator if you dont have php 5.4

    you probably want to start with

    class RecursiveCategoryIterator implements RecursiveIterator {...
    

    And that should contains a list of Category objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really need some help. I have a List which has 4 columns and
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some best practice advice here... Navigation based application. Root view is a UITableView
Need some help with a query.. I have three tables. Source id name 1
Need some help with DataFormatString in GridView. I have a Double value that needs
Say, I have a collection of some geolocations (in format Country > Region [
With some frequency I find myself writing APIs that offer an Iterator<Foo> which is
I need some help in returning an XmlDocument object from XPathNodeIterator. Here is what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.