Creating a recursive function in php to re-structure a recordset?

klsr

New Member
I was hoping someone might be able to advise on how best to do this.I have a database resultset that looks something like this:\[code\]PARENT CHILDCat TigerCat LionCat PantherMammal CatMammal DogCreature Mammal\[/code\]What I require is a function that when passed this resultset, will return something that looks like this:\[code\]PARENT CHILDCat TigerCat LionCat PantherMammal TigerMammal LionMammal PantherCreature TigerCreature LionCreature Panther\[/code\]To describe the process of such a function:
  • if a value in the CHILD field does not exist in the PARENT field, we simple add the row to $newArray.
  • if the value in the CHILD field does exist in the PARENT field, we want to examine any instances where this value occurs in the PARENT field and apply the same consideration, only adding to $newArray where the CHILD does not occur in the PARENT field, and associating the ultimate CHILD with the original PARENT.
I recognise this involves recursion while iterating over the parent field. I've attempted this but have ended up getting stuck...Thanks for any help!(Note that if is better served doing this using an SQL stored procedure, I am open to suggestions. I am using mySQL, and understood that recursion was not supported, hence me attempting this in php).
 
Back
Top