Find Greatest parent of list of categories

Hello

I am trying to solve something but got some issues.
I have a list of categories with sub-categories. A category can have up to 4 categories.

I don’t want to use the sub category id this is why i need to find the greatest parent related to the defined sub category.

For example with this data

     $data = [
            {
                "id": 1,
                "name" : 'Main category',
                "parent": 0,
            },
            {
                "id": 2,
                "name" : 'Sub Category 1',
                "parent": 1,
            },
            {
                "id": 3,
                "name" : 'Sub Category 2',
                "parent": 2,
            },
            {
                "id": 4,
                "name" : 'Sub Category 3',
                "parent": 3,
            },
        ]

I want to be able to find the greatest parent of Sub Category 3 (i=4) here this would mean the one with id=1.

Could someone help

I really can’t understand what you are asking. If we were to write a function to do this, what would the return value be?

Will there be multiple elements with a “name” of “Sub Category 3” and you want to find the highest value that the key “parent” has in any of those?

every category would have a unique id.
Categories can have multiple sub-category.
I was thinking about a recursive function thats checks wether the parent id is 0 or not.
In the end if for example i enter as input the category with id:4 I wish the output to be the object of greatest parent wich is the id:1 as it has itself no parent but is the grand grand father of Sub Category 3

Is it a bit more clear ? Thanks in advance

Sorry, no.

I wish the output to be the object of greatest parent wich is the id:1

Wouldn’t there be only one with an id of 1? Aren’t ids unique?

Are you saying that the “parent” is the id of that elements parent?

Can a parent have multiple children? That would sound like a tree structure to me.

Yeah as i said its a unique id , each category has a unique id and a parent id

Yes every parent can have multiple children and each children can have multiple children and so on. In my case I have up to 4 sub categories

I started writing something like that but obviously doesn’t restart and get deeper in the tree


                     function getParentId($repo,$id){
                        $result = [];
                        $data = $repo->findById($id);
                        $data = $data[0];

                        //base case
                        if($data->getParentId() == 0){
                            $result[] = $data;

                        } else {
                            $data = $repo->findById($data->getParentId());
                            $data = $data[0];
                            if($data->getParentId() == 0){
                                $result[] = $data;
                            } else {
                                getParentId($repo,$data->getId());
                            }
                        } 

                        return $result;

                     } 

Here i am using PHP

And here is the result (obviously at the end I only wish to have a single object

 {
         {
            "id": 455,
            "name": "Waterskiing and Wakeboarding",
            "adult": 0,
            "parentId": 447,
        },
        {
            "id": 447,
            "name": "Extreme Sports",
            "adult": 0,
            "parentId": 13,
        }
    }

Here my imput id was 455, its parentId is 447 but as its not 0 i need to search deeper. The parent of 447 is 13, still the parent isn’t 0 so i need to continue.

Hope you got it

Sorry, I guess I’m not smart enough to get what you want. Maybe someone else can help.

Sorry I wasn’t probably clear enough… Not always easy to explain things !
Still thanks again for trying.

UPDATE: Ahh, I see now, you have a parent id for each entry. Sorry, I haven’t had much sleep lately and so I missed that :slight_smile:

Since this is in an array, I think you would just iterate through the array element by element to find the parent. Keep doing that until you don’t find a parent. If you don’t find the parent then you know you have the “greatest parent”. You could have a function that finds the parent in the array and returns the object if it finds the parent or returns null/undefined/whatever php uses to represent no data if it doesn’t find the parent. Just keep calling this function, passing in the parent id you are looking for, until you don’t find the parent.

I found out two different approaches, first using recursion:

function getRootCategory($repo, $id) {
  $category = $repo->findById($id)[0];
  if ($category->getParentId() == 0) return $category;
  return getRootCategory($repo, $category->getParentId());
}

And using a while loop:

function getParentId($repo,$id){
 $category = $repo->findById($id)[0];
                   
 while($category->getParentId() !== 0) {
 $category = $repo->findById($category->getParentId())[0];
 }
                          
 return $category->getId();
} 

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.