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
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.
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
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.