I'm doing a select in my database by skipping a lot of information, and I'm saving the results in an array, which is organized this way: link
What I'm trying to do is sort the parents and children according to the level
shown in the image, placing the children inside a new array (called children
) of their respective parent.
For example ( link ):
0 => {
"ordination": "10501"
"level": 3
"children":
0 => {
"ordination": "1050101"
"level": 4
"children":
0 => {
"ordination": "1050101001"
"level": 5
}
1 => {
"ordination": "1050101002"
"level": 5
}
1 => {
"ordination": "1050102"
"level": 4
"children":
0 => {
"ordination": "1050101001"
"level": 5
}
}
}
I tried to think of a logic running through the array backwards, but it does not work when there are several level 5 elements, for example, so I could not finish.
I would like help with some logic for me to understand and program. Thanks!